in reply to while or for?
Okay, here's what I found:
$ perl -mO=Deparse -le 'print "$ARGV $_" for (<>)' foreach $_ (<ARGV>) { print "$ARGV $_"; }
COMPARE:
$ perl -mO=Deparse -le 'print "$ARGV $_" while (<>)' print "$ARGV $_" while defined($_ = <ARGV>);
Since $ARGV is set every time <ARGV> is evaluated, when you do it in a while loop, $ARGV is set correctly for each file in the loop. But using a for loop, you slurp <ARGV> in one shot, then iterate over $_, so $ARGV is set to the last file that was iterated over the last time <ARGV> was evaluated...
Hope that makes sense...
Update: I guess I just repeated what broquaint already said, but since I didn't understand what he wrote, I posted this, thinking I had something new to offer. Perhaps someone else will find it easier to understand, too...
--
|
|---|