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

发布于 2024-10-25 09:34:42 字数 628 浏览 1 评论 0 原文

我无法让这行代码正常工作:

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”错误,因为我使用的是 strict。我还注意到他们在示例中缺少 ; ,所以我猜测除此之外可能还有更多错误。

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

I'm having trouble getting this line of code to work:

for my $fh (FH1, FH2, FH3) { print $fh "whatever\n" }

I found it at perldoc but it doesn't work for me.

The code I have so far is:

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"; }

I'm getting "Bareword" errors on the (fh1, fh2) part because I'm using strict. I also noticed they were missing a ; in the example, so I'm guessing there might be some more errors aside from that.

What's the correct syntax for printing to two files at once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

狼性发作 2024-11-01 09:34:42

你还没有打开这些文件。

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"; }

但现代的方法是前者。

You haven't opened the files.

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"; }

Notice that I'm not using barewords. In the olden days, you would have used:

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

but the modern approach is the former.

亚希 2024-11-01 09:34:42

我只会使用 IO::Tee

use strict;
use warnings;
use autodie; # open will now die on failure
use IO::Tee;

open my $fh1, '>', 'file1';
open FH2, '>', 'file2';

my $both = IO::Tee->new( $fh1, \*FH2 );

print {$both} 'This is file number ';

print {$fh1} 'one';
print FH2    'two';

print {$both} "\n";
print {$both} "foobar\n";

$both->close;

运行上述程序会产生:

file1

This is file number one
foobar

file2

This is file number two
foobar

我建议阅读整个 perldoc 文件更高级的用法。

I would just use IO::Tee.

use strict;
use warnings;
use autodie; # open will now die on failure
use IO::Tee;

open my $fh1, '>', 'file1';
open FH2, '>', 'file2';

my $both = IO::Tee->new( $fh1, \*FH2 );

print {$both} 'This is file number ';

print {$fh1} 'one';
print FH2    'two';

print {$both} "\n";
print {$both} "foobar\n";

$both->close;

Running the above program results in:

file1

This is file number one
foobar

file2

This is file number two
foobar

I would recommend reading the whole perldoc file for more advanced usage.

戒ㄋ 2024-11-01 09:34:42

看起来不错,只是以前在 Perl 中使用裸字作为文件句柄很常见,但现在建议使用普通标量。

因此,请确保您确实打开了文件,然后只需将 (fh1, fh2) 部分替换为实际的文件句柄(即 ($fh1, $fh2) > 或者什么)

That looks about right, it's just that it used to be common in Perl to use barewords as file handles, but nowadays it's recommended to use normal scalars.

So make sure that you actually have the files open, then just substitute the (fh1, fh2) part with the actual file handles (which would be ($fh1, $fh2) or something)

少女净妖师 2024-11-01 09:34:42

另一个版本基于布莱恩的回答:

open(my $fh1, ">", $stat_file) or die "Couldn't open $stat_file!";
open(my $fh2, ">", $stat_file2) or die "Couldn't open $stat_file2!";
for ($fh1, $fh2) { print $_ "whatever\n"; }

another version based off of Brian's answer:

open(my $fh1, ">", $stat_file) or die "Couldn't open $stat_file!";
open(my $fh2, ">", $stat_file2) or die "Couldn't open $stat_file2!";
for ($fh1, $fh2) { print $_ "whatever\n"; }
几味少女 2024-11-01 09:34:42

您首先需要打开文件以获得有效的文件句柄

open (MYFILEA, $stat_file);
open (MYFILEB, $stat_file2);
for my $fh ( \*MYFILEA, \*MYFILEB ) { print $fh "whatever\n" } 
close (MYFILEA);
close (MYFILEB); 

You first need to open the file in order to get valid filehandles

open (MYFILEA, $stat_file);
open (MYFILEB, $stat_file2);
for my $fh ( \*MYFILEA, \*MYFILEB ) { print $fh "whatever\n" } 
close (MYFILEA);
close (MYFILEB); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文