basin
basin

Reputation: 4190

reset state variables on next file in `perl -n` one-liner

I'm processing multiple files with find ... | xargs perl -ne and when I proceed to next file I need to reset some variables like gawk 'BEGINFILE {}' does.

As a workaround, I check that the current filename changed. Is there a cleaner way?

if ($oldARGV ne $ARGV) { $oldARGV = $ARGV; $var1=""; ... } ... 

Upvotes: 3

Views: 197

Answers (1)

Shawn
Shawn

Reputation: 52419

Using eof with no argument (Or with eof ARGV):

$ perl -nE 'say "Done with file $ARGV" if eof' *.txt
Done with file a.txt
Done with file b.txt

Upvotes: 7

Related Questions