微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl – 如何一次打印成两个文件?

我无法让这行代码工作:
for my $fh (FH1,FH2,FH3) { print $fh "whatever\n" }

我在perldoc找到它但它对我不起作用.

我到目前为止的代码是:

my $archive_dir = '/some/cheesy/dir/';
my ($stat_file,$stat_file2) = ($archive_dir."file1.txt",$archive_dir."file2.txt");
my ($fh1,$fh2);

for my $fh (fh1,fh2) { print $fh "whatever\n"; }

我在(fh1,fh2)部分出现“Bareword”错误,因为我使用严格.我也注意到他们错过了一个;在这个例子中,所以我猜可能会有更多的错误.

一次打印到两个文件的正确语法是什么?

解决方法

您尚未打开文件.
my ($fh1,$fh2);
open($fh1,">",$stat_file) or die "Couldn't open $stat_file: $!";
open($fh2,$stat_file2) or die "Couldn't open $stat_file2: $!";

for my $fh ($fh1,$fh2) { print $fh "whatever\n"; }

请注意,我没有使用裸字.在过去,你会使用:

open(FH1,">$stat_file");
...
for my $fh (FH1,FH2) { print $fh "whatever\n"; }

但现代的方法是前者.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐