Rakuvs.Perl5,一个意想不到的结果
当我在其中运行此脚本时,Raku我会收到A带有几个换行符的信件。为什么我没有按预期(和 Perl5 一样)获得连接的字符串?
编辑顺便说一句,我可以使用编译器在commaIDE文件中进行Perl5编译,我可以在哪里将此编译器更改为Perl5?
say "A";
my $string1 = "00aabb";
my $string2 = "02babe";
say join ("A", $string1, $string2);
print "n";
my @strings = ($string1, $string2);
say join ("A", @strings);
print "n";
回答
问题是函数名后面的空格。如果你在那里留一个空格,raku 会将括号中的表达式视为一个 List,它是一个单一的事物。在这里 join 会认为您想将列表用作加入者。
观察:
>raku -e "sub foo( $arg ) { say $arg.WHAT }; foo( 'abc', )"
(Str)
>raku -e "sub foo( $arg ) { say $arg.WHAT }; foo ( 'abc', )"
(List)
所以简而言之,如果您想使用括号来调用子程序,请不要在子程序名称和开头括号之间放置空格。
回答
我建议的解决方案是删除括号:
say "A";
my $string1 = "00aabb";
my $string2 = "02babe";
say join "A", $string1, $string2; # Pass THREE arguments to `join`, not ONE
print "n";
my @strings = $string1, $string2;
say join "A", @strings;
print "n";
(@strings第二次调用 中的元素join被展平,因此与第一次调用 的方式相同join。)
上面的代码显示:
A
00aabbA02babe
00aabbA02babe
当我在其中运行此脚本时,
Raku我会收到A带有几个换行符的信件。
您的代码join使用一个参数(即连接器)和要连接的零个字符串调用。所以join调用生成空字符串。因此你会得到空行。
为什么我没有得到连接的字符串
say join...代码中的两个语句只打印一个换行符,因为它们类似于say下面的第三行和第四行:
say join( " o/ ", "one?", "two?" ); # one? o/ two??
say join " o/ ", "one?", "two?" ; # one? o/ two??
say join ( " o/ ", "one?", "two?" ); # ?
say join( " o/ one? two?" ); # ?
上面的第一行和第二行将三个字符串传递给join. 第三行传递一个单一的 List,然后被强制成为一个单一的字符串(List使用单个空格字符连接的元素的串联),即与第四行相同的结果。
该my @strings = ($string1, $string2);咒语恰好工作按预期,因为分配给一个“复数”变量上的左侧=将迭代的值,或值的列表,在右手边。
但是在 Raku 中避免冗余代码是一个好习惯,在这种情况下,只有在您真的必须使用括号来表达与没有它们的代码不同的东西时才使用括号。这是 Raku 中使代码高信号、低噪声的一般原则。对于您的代码,所有括号都是多余的。
- Given that this is the accepted answer, it may be worth saying up front that the whitespace between sub name and paren is significant.
- Thx @donaldh. That's what I meant by "(with no space between the end of the name and the opening paren)" but I've already concluded my answer is sloppy in several ways that I hope to fix tomorrow, and actual code is always better for that sort of thing than prose, so I'll add code to clarify that and/or link to Holli's answer.
回答
请参阅要避免的陷阱,请尝试:
my @s=("00aabb", "02babe");
say join "A", @s;
say join |("A",@s);
say join("A",@s);
say @s.join: "A";
#or some crazy
say join ("A",@s);
say "A".&join: @s;
say join @s: "A";
say "A" [&join] @s;
say @s R[&join] "A";
#…