在 Raku 中替代 Perl 的 <>?
Alternative to Perl's <> in Raku?
在这里学习我关于 Raku(neé Perl 6)的方法,各方面都很好。但我非常怀念 Perl 5 的魔力 <>
,在那里你可以:
my $x = <>;
print $x;
while(<>) {
print join(':', split);
}
(将下一个输入行读入 $x
,遍历其余行;输入来自命名为输入的文件,如果没有给出文件,则为标准输入)。 "Perl 5 to 6" tutorials/migration 指南/...只是讨论 slurp
整个文件,或 open
按名称对单个文件进行处理。没有魔法"take input from named files in sequence"我能找到
我要魔法回来!
魔法够你用吗?
sub MAIN( Str $file where *.IO.f )
{
.say for $file.IO.lines.map: *.comb.join(':');
}
您要找的功能基本上已经存在。这个脚本:
my $x = get();
say "First: $x";
for lines() {
.say
}
给定这些输入文件:
$ cat foo
foo line 1
foo line 2
$ cat bar
bar line 1
bar line 2
Will,调用时为:
raku script.p6 foo bar
产生输出:
First: foo line 1
foo line 2
bar line 1
bar line 2
如果没有文件,它也会从 $*IN
获取输出。唯一不存在的是 <>
的单一替代品,因为这将取决于 wantarray
类功能,这与多重分派不兼容(Raku 认为多重分派更有用) .
get
和 lines
的零参数候选已实现 in terms of $*ARGFILES
,一个文件句柄,提供从参数列表或 [=14] 中获取文件的功能=] - 意思是可以将它传递给任何需要文件句柄的代码。
在这里学习我关于 Raku(neé Perl 6)的方法,各方面都很好。但我非常怀念 Perl 5 的魔力 <>
,在那里你可以:
my $x = <>;
print $x;
while(<>) {
print join(':', split);
}
(将下一个输入行读入 $x
,遍历其余行;输入来自命名为输入的文件,如果没有给出文件,则为标准输入)。 "Perl 5 to 6" tutorials/migration 指南/...只是讨论 slurp
整个文件,或 open
按名称对单个文件进行处理。没有魔法"take input from named files in sequence"我能找到
我要魔法回来!
魔法够你用吗?
sub MAIN( Str $file where *.IO.f )
{
.say for $file.IO.lines.map: *.comb.join(':');
}
您要找的功能基本上已经存在。这个脚本:
my $x = get();
say "First: $x";
for lines() {
.say
}
给定这些输入文件:
$ cat foo
foo line 1
foo line 2
$ cat bar
bar line 1
bar line 2
Will,调用时为:
raku script.p6 foo bar
产生输出:
First: foo line 1
foo line 2
bar line 1
bar line 2
如果没有文件,它也会从 $*IN
获取输出。唯一不存在的是 <>
的单一替代品,因为这将取决于 wantarray
类功能,这与多重分派不兼容(Raku 认为多重分派更有用) .
get
和 lines
的零参数候选已实现 in terms of $*ARGFILES
,一个文件句柄,提供从参数列表或 [=14] 中获取文件的功能=] - 意思是可以将它传递给任何需要文件句柄的代码。