The syntax
while( <> ) {
}
Will 'automagically' run through every line of each of the files the user specified on the command line. It is the equivalent of:
foreach $ARGV (@ARGV) {
open( THISFILE, $ARGV ) || next;
while( $_ = <THISFILE> ) {
}
}
Which is why your script was executing many times per file. UNIX programs traditionally don't automatically create new files from their input, they just slurp in all the input files they're given and then print output directly to STDOUT so it can be redirected. Consider:
$ grep 'foo' thisfile.txt thisOtherFile.txt
Which will run through all the files it's given and print matching lines. Perl allows the programmer to do this kind of task easily with the <> syntax.