Some simple Inline::Perl5 code returns a list, but it seems to return the count of the items rather than the actual list.
Changing the number of items involved changes the count.
use Inline::Perl5;
my $p5 = Inline::Perl5.new;
my $perl5_code = q:to/END/;
sub p5_data {
my @monsters = qw( godzilla blob tingler kong dorisday );
return @monsters;
}
p5_data();
END
my @stuff = $p5.run: $perl5_code;
say @stuff; # [5]
I thought I'd get the list of strings stored in the array, but instead it acts like something is switching it to scalar context.
Update:
ikeami points out that it works better to return the reference to the array:
return \@monsters;
Though, then you end up with an array in the first element of the @stuff array when you do this:
my @stuff = $p5.run: $perl5_code;
An alternate approach (from reading the Inline::Perl5 docs), is after doing a $p5.run
to define the perl5 sub, to call it from perl6:
my @stuff = $p5.call('p5_data');
Then the list return (return @monsters;
) gets loaded into the
array as I expected:
[godzilla blob tingler kong dorisday]
This is a recently installed Inline::Perl5 of version 0.40, on "Rakudo Star version 2019.03.1 built on MoarVM version 2019.03 implementing Perl 6.d".
Update2: So, it seems that "run" implies a scalar context and "call" is a list context.
use Inline::Perl5;
my $p5 = Inline::Perl5.new;
my $perl5_defsub = q:to/END/;
sub whadaya_want {
wantarray() ? 'LIST' : 'SCALAR';
}
END
$p5.run: $perl5_defsub;
my $run_context = $p5.run( 'whadaya_want' );
my $call_context = $p5.call( 'whadaya_want' );
say "run: ", $run_context;
say "call: ", $call_context;
# run: SCALAR
# call: LIST