in reply to perl doesn't like variable

By the way, there was no need to use $counter at all. Perl has a variable $. which tracks the counter of the last accessed file handler. Have more than one file open? okay, then use FOO->input_line_number(). Play with the following code (save this piece of code as test.pl):

use IO::Handle; open(MYSELF1, "<", "test.pl"); open(MYSELF2, "<", "test.pl"); while (<MYSELF1>) { print "after reading from myself1: $.\n"; if ($. % 2) { <MYSELF2>; print "after reading from myself2: $.\n"; } print "returned values from input_line_number: ("; print MYSELF1->input_line_number(), ", "; print MYSELF2->input_line_number(), ")\n"; } close(MYSELF1); close(MYSELF2);