从 Inline::Perl5 返回的列表给出了项目的计数,而不是列表 [英] list return from Inline::Perl5 gives a count of items, not the list
问题描述
一些简单的 Inline::Perl5 代码返回一个列表,但它似乎返回项目的计数而不是实际列表.
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.
更新:
ikeami 指出返回对的引用效果更好数组:
ikeami points out that it works better to return the reference to the array:
return \@monsters;
不过,你最终会在数组的第一个元素中得到一个数组执行此操作时@stuff 数组:
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;
另一种方法(来自阅读 Inline::Perl5 文档)是在执行 $p5.run
之后定义 perl5 子,从 perl6 调用它:
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');
然后列表返回(return @monsters;
)被加载到数组如我所料:
Then the list return (return @monsters;
) gets loaded into the
array as I expected:
[godzilla blob tingler kong dorisday]
这是最近安装的 0.40 版的 Inline::Perl5,在《乐堂之星 2019.03.1 版构建于 MoarVM 2019.03 版实现Perl 6.d".
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: 所以,似乎运行"暗示标量上下文和调用"是一个列表上下文.
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
推荐答案
Perl5::Inline
将返回值放入标量上下文中.
Perl5::Inline
puts return values into scalar context.
作为背景,在 Perl 5 中,上下文向内流入例程,因此例程总是知道它在哪个上下文中.
As background, in Perl 5, context flows inwards into routines, so a routine always knows which context it's in.
在 Perl 6 中,上下文向外流动,因此例程返回一个对象,该对象在不同的上下文中表现不同.
In Perl 6, context flows outwards, so a routine returns an object that can behave differently in different context.
Perl 5 和 Perl 6 之间的这种阻抗不匹配意味着 Inline::Perl5
必须决定在一个上下文中调用 Perl 5 例程,这就是标量.
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.
正如池上所指出的,正确的解决方案是返回一个正确的标量,也就是引用(在 Perl 5 中).Inline::Perl5
中的限制可能意味着您需要在 Perl 6 端显式取消引用.
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.
这篇关于从 Inline::Perl5 返回的列表给出了项目的计数,而不是列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!