Bompa wrote:
"I wrote a script to demonstrate how the value of $_ could get changed
unexpectedly. (It took me an hour, but I learned from it, heh).
use strict;
our @list = qw(a b c d);
foreach (@list) {
&check_b;
print $_, "\n";
}
sub check_b {
foreach (@list) {
#local $_;
$_ =~ s/b/bb/;
# SAVE TO FILE
}
}
As is, @list is printed incorrectly, but if you uncomment #local $_;,
it'll print as expected."
Bompa, was this the actual code you used? what was your output? and what
machine are you on?
1 #!/usr/bin/perl -w
2 use strict;
3 use diagnostics;
4
5 our @list = qw(a b c d);
6 foreach (@list) {
7 &check_b;
8 print $_, "\n";
9 }
10
11
12 sub check_b {
13 foreach (@list) {
14 local $_;
15 $_ =~ s/b/bb/;
16 # SAVE TO FILE
17 }
18 }
~
with #13 uncommented it produces the following output:
Use of uninitialized value in substitution (s///) at ./ccdd line 15 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was
interpreted as a "" or a 0, but maybe it was a mistake. To suppress
this
warning assign a defined value to your variables.
a
b
c
d
with 'use diagnostics' removed it produces:
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
a
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
b
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
c
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
Use of uninitialized value in substitution (s///) at ./ccdd line 14.
d
I am running perl 5.6 on macos x.1. Sorry dude, i'm not trying to be a
dick or anything, just asking ;-)