list return from Inline:Perl5 gives a count of items, not the list

Moritz's answer explains IP5's dilemma but, aiui, underestimates niner's ingenuity. :)

Based on my understanding of exchanges with niner about this a while back, the current IP5 README, and updates in Joseph's answer, IP5 provides four options.


Call a P5 sub in list context:

Inline::Perl5.new.call( 'subname', arg1, arg2 ... )

See IP5: Call a Perl 5 function.


Call a P5 sub in scalar context:

Inline::Perl5.new.run( 'subname arg1, arg2 ...' )

or

EVAL 'subname arg1, arg2 ...', :lang<Perl5>

See IP5: Run arbitrary Perl 5 code.


Call a P5 method in list context:

Use regular P6 syntax:

$p5object.method( arg1, arg2 ... )

to call the method method on a Perl 5 object held in $p5object passing it arg1 and arg2 as its first two arguments.

See IP5: Invoke a method on a Perl 5 object.


Call a P5 method in scalar context:

Use regular P6 syntax but also insert Scalar as an extra first positional argument:

$p5object.method( Scalar, arg1, arg2 ... )

to call the method method on a Perl 5 object held in $p5object passing it arg1 and arg2 as its first two arguments.

IP5 removes the Scalar before calling the Perl 5 method.

See IP5: Invoking a method in scalar context.


Dealing with problems with the Inlines

Imo SO posts are an ideal way to develop a knowledge base surrounding use of Inlines with P6. The following is in no way meant to discourage posting questions.

(Indeed, it is in no way meant to discourage posting questions and answering your own question -- "it is not merely OK to ask and answer your own question, it is explicitly encouraged" with "a feature built into SO just for this" to "Answer your own question – share your knowledge, Q&A-style".1 I think this approach is especially appropriate for the [perl6] tag and doubly so for problems and solved problems related to the Inlines.)

With the foregoing out the way, see also the sections starting at What to expect when using foreign language adaptors of my answer to Perl6: getting array ref for Perl5 Module for my attempt at a mini generic guide on dealing with similar problems with P6 Inlines. It's just a first attempt and I'd love to get feedback, especially about problems with it, so if you read it, please consider commenting there if any of the guide seems too much, too little, or too confusing in any respect. TIA.

Foonotes

1 In a comment below @BradGilbert has recommended a delay between posting a question and answering it. The merit I can see to that is to allow time for folk to give feedback on how well worded the question is and perhaps see if someone chooses to write an even better answer than the one you had in mind. But if you do plan to delay answering, please say at the top of your question that you plan to answer it.


Perl5::Inline puts return values into scalar context.

As background, in Perl 5, context flows inwards into routines, so a routine always knows which context it's in.

In Perl 6, context flows outwards, so a routine returns an object that can behave differently in different context.

This impedance mismatch between Perl 5 and Perl 6 means that Inline::Perl5 has to decide to call Perl 5 routines in one context, and that's scalar.

As ikegami pointed out, the proper solution is to return a proper scalar, aka reference (in Perl 5 speak). Limitations in Inline::Perl5 might mean you need to explicitly dereference on the Perl 6 side.

Tags:

Perl

Raku