简体   繁体   English

Perl单行代码:如何跳至@ARGV中的下一个文件

[英]Perl one-liner: how to skip to the next file in @ARGV

I was thinking about how to "short circuit" reading a file within a perl -ne loop. 我正在考虑如何“短路”在perl -ne循环中读取文件。 If I hit some condition, I want to abandon the current file and proceed to the next one in @ARGV. 如果遇到某种情况,我想放弃当前文件并继续执行@ARGV中的下一个文件。 This would be similar to GNU awk's handy nextfile command. 这类似于GNU awk的方便的nextfile命令。

Something like this: 像这样:

perl -ne 'do_something(); next_file if some_condition()' files ...

Playing around, I found that close ARGV achieves the "go to next file" goal. 闲逛,我发现close ARGV可以达到“转到下一个文件”的目标。

Are there other, less magical, ways to accomplish this? 还有其他更神奇的方法来做到这一点吗?

Nope, close ARGV is the right way to do this. 不,关闭ARGV是执行此操作的正确方法。

As noted by glenn jackman, this has the side effect of resetting your $. 如glenn jackman所述,这具有重置$.的副作用$. line counter (which normally continues incrementing across several files when using ARGV), so you may want to save and restore it: 行计数器(使用ARGV时,它通常会在多个文件中继续递增),因此您可能需要保存和恢复它:

my $line = $.;
close ARGV;
$. = $line;

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

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