简体   繁体   中英

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. If I hit some condition, I want to abandon the current file and proceed to the next one in @ARGV. This would be similar to GNU awk's handy nextfile command.

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.

Are there other, less magical, ways to accomplish this?

Nope, close ARGV is the right way to do this.

As noted by glenn jackman, this has the side effect of resetting your $. line counter (which normally continues incrementing across several files when using ARGV), so you may want to save and restore it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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