简体   繁体   English

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

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

How would one be able to do this? 一个人怎么能做到这一点? I know you have to use <>.I already tried giving it a shot,but it might just because I am on Windows. 我知道您必须使用<>。我已经尝试过试一下,但这可能只是因为我在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;

You didn't state whether you need to treat the files differently or not. 您没有说明是否需要区别对待文件。 If not, then you can just use <> without needing to call open explicitly. 如果没有,那么您可以使用<>而不需要显式调用open

while (<>) {
  # Process $_
}

will work fine. 会很好的工作。 When it gets to the end of the first file, <> will continue on with reading the second file. 当到达第一个文件的末尾时, <>将继续读取第二个文件。 And so on until it runs out of files to read. 依此类推,直到用完所有要读取的文件。

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

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