我有一个文件名列表。我必须为每个名称创建一个文件,将行写入各种文件(无特定顺序),然后关闭它们。
我怎么能在 perl 中做到这一点?我设想类似下面的代码(它不会以这种形式工作并给出语法错误):
my @names = qw(foo.txt bar.txt baz.txt);
my @handles;
foreach(@names){
my $handle;
open($handle, $_);
push @handles, $handle;
}
# according to input etc.:
print $handles[2] "wassup";
print $handles[0] "hello";
print $handles[1] "world";
print $handles[0] "...";
foreach(@handles){
close $_;
}
我怎样才能做到这一点?