When I run the code, above, I get this output: (The local directory contains 2 *.txt files: test1.txt and test2.txt.)1 #! /usr/bin/perl -w 2 3 my @test_files = glob "*.txt"; 4 5 foreach (@test_files) { 6 print "Starting processing of file, $_.\n"; 7 open TEST_FILE, "< $_" or die "Couldn't open test file, '${_ +}', for reading: ($!)"; 8 9 while (<TEST_FILE>) { 10 print; 11 } 12 13 print "Ending processing of file, $_.\n"; 14 }
Note the missing filenames at the ends of both "Ending processing..." lines, as well as the warnings immediately preceeding them. Here are the contents of "test1.txt" and "test2.txt":Starting processing of file, test1.txt. Line 1 from 'test1.txt' Line 2 from 'test1.txt' Line 3 from 'test1.txt' Use of uninitialized value in concatenation (.) or string at ./test.pl + line 13, <TEST_FILE> line 3. Ending processing of file, . Starting processing of file, test2.txt. Line 1 from 'test2.txt' Line 2 from 'test2.txt' Line 3 from 'test2.txt' Use of uninitialized value in concatenation (.) or string at ./test.pl + line 13, <TEST_FILE> line 6. Ending processing of file, .
test2.txt:Line 1 from 'test1.txt' Line 2 from 'test1.txt' Line 3 from 'test1.txt'
Line 1 from 'test2.txt' Line 2 from 'test2.txt' Line 3 from 'test2.txt'
In reply to $_ getting clobbered by inner loop. by dbanas
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |