加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 21:52:19 所属栏目:大数据 来源:网络整理
导读:我无法让这行代码工作: for my $fh (FH1,FH2,FH3) { print $fh "whatevern" } 我在perldoc找到它但它对我不起作用. 我到目前为止的代码是: my $archive_dir = '/some/cheesy/dir/';my ($stat_file,$stat_file2) = ($archive_dir."file1.txt",$archive_dir
我无法让这行代码工作:
for my $fh (FH1,FH2,FH3) { print $fh "whatevern" }

我在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 "whatevern"; }

我在(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 "whatevern"; }

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

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

但现代的方法是前者.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读