On Tue, Jul 05, 2005 at 08:05:11AM +0200, Jean-Luc VERIT wrote: > But > you told me "Dont used $_ in nested loops", But I wrote thousands lines of > Perl, and I used and abused using $_ in nested loops, and the > only time it didn't work is when it is used with this sequence. > first "$_" related to name of file from an array with a foreach, > second open this file, > third read this file with the second $_ and "while(<FIL>){ ....." > > It is said some where in the Camel book (in the second and in the third > edition) that each loop has its own instance of "$_", and it always works > like this, but in the case I mentionned.
Only for loops auto-localize $_. A while loop has no intrinsic $_ assignment. Its a bit of magic associated with the <FH> operator. This is the reason you only seem to run into the problem while reading files, its rare to use $_ in a while loop otherwise. It may be worthwhile investigating if auto-localizing $_ in a while(<FH>) construct is worth adding to the language. Anyhow, stick to "foreach my $thing (@things)" for anything but the smallest loops and you'll be ok. > An other point (may be related to this) : I wrote about 2 or 3 hundred of > thousands lines of Perl, but I'm still confused with FILES in PERL. It seems > to me to be like a patch added to the language. It could be an explanation > of the problem, as the first instance of "$_"is created by a "foreach" > (which seems to me very robust), ans the second instance is created by the > "while(<FIL>){ ....." Let's just say file handling is showing its age and hacks. -- Michael G Schwern [EMAIL PROTECTED] http://www.pobox.com/~schwern Just call me 'Moron Sugar'. http://www.somethingpositive.net/sp05182002.shtml