我想知道冒号与Perl 6中的方法和函数调用有什么关系.对于记录,我使用的是基于MoarVM版本2015.05构建的perl6版本2015.05-55-gd84bbbc.
我刚刚在Perl6规范测试中看到了以下内容(S32-io)(我添加了评论):
$fh.print: "0123456789A"; # prints '0123456789A' to the file
Run Code Online (Sandbox Code Playgroud)
据我所知,这相当于:
$fh.print("0123456789A"); # prints '0123456789A' to the file
Run Code Online (Sandbox Code Playgroud)
这两个似乎都采取了多个参数并平整列表:
$fh.print: "012", "345", "6789A"; # prints '0123456789A' to the file
$fh.print("012", "345", "6789A"); # prints '0123456789A' to the file
my @a = <012 345 6789A>;
$fh.print(@a); # prints '0123456789A' to the file
$fh.print: @a; # prints '0123456789A' to the file
Run Code Online (Sandbox Code Playgroud)
必须有一些理由拥有这两种不同的语法.有没有理由使用一种或另一种语法?
我还注意到,当用作方法时,我们必须使用:或()使用print:
$fh.print(@a); # Works
$fh.print: @a; # Works!
$fh.print @a; # …Run Code Online (Sandbox Code Playgroud) perl6 ×1