3

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=""; ... } ... 
basin
  • 3,949
  • 2
  • 27
  • 63
  • @Dawg sorry I meant `gawk` `BEGINFILE` – basin Oct 21 '20 at 14:11
  • 2
    Why are you using xargs? Perl is perfectly capable of processing multiple files, and it is much cleaner to handle arguments inside Perl than try to compare old arguments to xargs. – TLP Oct 21 '20 at 15:28
  • Re "*Perl is perfectly capable of processing multiple files*", @TLP, You just answered your own question :) On a non-GNU system, you'd want to `xargs` to take advantage of that ability. See [Specifying file to process to Perl one-liner](https://stackoverflow.com/q/41742890/589924) – ikegami Oct 21 '20 at 20:34

1 Answers1

7

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
dawg
  • 98,345
  • 23
  • 131
  • 206
Shawn
  • 47,241
  • 3
  • 26
  • 60
  • @basin, I'm not sure what you mean. There are no lines in empty files, so the loop is never entered for an empty file, so how can code that is never executed "not work"? – ikegami Oct 21 '20 at 20:31
  • @ikegami I think they meant to say that -- that this way they won't see the empty files. – zdim Oct 22 '20 at 00:10
  • @basin the `@ARGV` contains names of all files given on the command line. Then it'd just be about how to organize your code to track files, and combining `eof` and `@ARGV` may well come out quite clear and nice to look at. – zdim Oct 22 '20 at 00:12
  • @zdim, Re "*that this way they won't see the empty files*", Except the OP is already using `ARGV`, so they're already skipping empty files, and they didn't ask to change that. There's some conflicting information here, so clarification is needed. – ikegami Oct 22 '20 at 02:33