Anytime you make a direct assignment to the special variable $_, you should local-ize it. This code demonstrates the problem:
#!/usr/bin/perl
use strict; use warnings;
my @array = (1..5);
sub nl { $_ = "\n"; print; }
foreach (@array) { nl; print; }
__END__
The foreach assigns each element of @array to the special variable $_. Then the function nl() is called. The first thing it does is assign a value to $_ which clobbers the previous value. That value is then printed as the implied default argument to print. Control returns from the function and print is called again with the implied default value in $_ which is still the value assigned in nl rather than the value assigned by foreach.
Localizing by changing the assignment in nl() to:
local $_ = "\n";
results in the correct output. The assignment is made local to the enclosing scope, which in this case is the nl() function. When that scope is entered the old value of $_ is saved before any assignment is made. Then when the scoped is exited, the old value is restored.
Randy.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>