in reply to Read contents of multiple files into new file and then move source files?

while (<>) {

reads all the files in @ARGV in one go (emptying @ARGV while doing so), so you get to execute the foreach loop only once...

One solution would be to open the files individually using open.   Another would be to copy @ARGV into another array, and do the moving afterwards.

  • Comment on Re: Read contents of multiple files into new file and then move source files?
  • Download Code

Replies are listed 'Best First'.
Re^2: Read contents of multiple files into new file and then move source files?
by shadowfox (Beadle) on Feb 10, 2012 at 19:56 UTC
    Thanks, that’s what I was missing, I didn't realize the array was emptied before the handle was closed. Changing the middle portion of the code as suggested works fine, just needed to save the array in another array for later use.
    @ARGV = glob('*.txt'); if (@ARGV){ my @MOVE = @ARGV; while (<>) { print $FH $_; $COUNT++; } foreach $FILE (@MOVE) { move($FILE, $OUTDIR) or die "Could not move $FILE to $ +OUTDIR: $!\n"; } }