繁体   English   中英

如何在Perl中从命令行读取两个文件?

[英]How can I read two files from command line in Perl?

一个人怎么能做到这一点? 我知道您必须使用<>。我已经尝试过试一下,但这可能只是因为我在Windows上。

# run as:
#       perl my_script1.pl file1 file2
my ($file1, $file2) = @ARGV;
open my $fh1, '<', $file1;
open my $fh2, '<', $file2;
while (<$fh1>) {
    ... do something with $_ from $file1 ...
}
while (<$fh2>) {
    ... do something with $_ from $file2 ...
}
close $fh1;
close $fh2;

您没有说明是否需要区别对待文件。 如果没有,那么您可以使用<>而不需要显式调用open

while (<>) {
  # Process $_
}

会很好的工作。 当到达第一个文件的末尾时, <>将继续读取第二个文件。 依此类推,直到用完所有要读取的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM