perl6/rakudo:取消引用问题
#!perl6
use v6;
my $list = 'a' .. 'f';
sub my_function( $list ) {
for ^$list.elems -> $e {
$list[$e].say;
}
}
my_function( $list );
首先我在 perl5 风格中尝试了这个,但它不起作用:
for @$list -> $e {
$e.say;
}
# Non-declarative sigil is missing its name at line ..., near "@$list -> "
How can I do this in perl6?
#!perl6
use v6;
my $list = 'a' .. 'f';
sub my_function( $list ) {
for ^$list.elems -> $e {
$list[$e].say;
}
}
my_function( $list );
First I tried this in perl5-style, but it didn't work:
for @$list -> $e {
$e.say;
}
# Non-declarative sigil is missing its name at line ..., near "@$list -> "
How could I do this in perl6?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Perl 6 中,您不会像这样取消引用变量。只需使用
for $list
但这可能不会做您想做的事情。
'a'..'f'
在 Perl 6 中并不构造列表,而是构造一个名为Range
的内置数据类型。您可以使用say $list.WHAT
进行检查。要将其转换为列表并迭代每个元素,您可以使用for $list.list
You don't dereference variables like this in Perl 6. Just use
for $list
But that proably won't do what you want to do.
'a'..'f'
doesn't construct a list in Perl 6, but rather a built-in data type calledRange
. You can check that withsay $list.WHAT
. To turn it into a list and iterate over each element, you'd usefor $list.list
这些应该可以工作:
由于
$list
是一个标量,因此for $list
将仅迭代单个项目。These should work:
Since
$list
is a scalar,for $list
will just iterate over a single item.现在,Rakudo 2015.02 可以正常工作了。
你最好使用
@
作为变量名的twigil作为数组。Perl 6 是上下文相关的语言,因此如果您希望数组充当“真正的数组”,您最好给它一个合适的名称。
Now, Rakudo 2015.02 works it ok.
You'd better use
@
as twigil of variable name as array.Perl 6 is context sensitive language, so if you want array act as 'true array', you'd better give it a suitable name.