J
John Brock
I seem to have misunderstood something about 'my'. I was under
the impression that when you defined a variable in a function or
block using 'my' the variable was totally private and totally
isolated from anything that happens anywhere else. In particular
I thought that unless I explicitly gave the variable a value it
would start out undefined every time the function or block was
called. But in fact this does not seem to be the case, as the
following script illustrates:
=== Begin script ===
sub zzz {
my @aaa;
print "defined: '", defined(@aaa), "', last: $#aaa\n";
@aaa = (1, 2, 3);
}
zzz();
zzz();
=== End script ===
I would have expected that both calls to zzz() would produce exactly
the same output. But in fact what I get, using Perl 5.8.0 under
Solaris, is:
=== Begin output ===
defined: '', last: -1
defined: '1', last: -1
=== End output ===
In the first invocation of zzz() array @aaa start out undefined,
while in the second @aaa starts out as a defined array with length
0. This is not what I expected at all! I haven't been able to
find this behavior written up anywhere, so is it possible it is a
bug? If not, can anyone explain what is happening, or point me to
an explanation?
the impression that when you defined a variable in a function or
block using 'my' the variable was totally private and totally
isolated from anything that happens anywhere else. In particular
I thought that unless I explicitly gave the variable a value it
would start out undefined every time the function or block was
called. But in fact this does not seem to be the case, as the
following script illustrates:
=== Begin script ===
sub zzz {
my @aaa;
print "defined: '", defined(@aaa), "', last: $#aaa\n";
@aaa = (1, 2, 3);
}
zzz();
zzz();
=== End script ===
I would have expected that both calls to zzz() would produce exactly
the same output. But in fact what I get, using Perl 5.8.0 under
Solaris, is:
=== Begin output ===
defined: '', last: -1
defined: '1', last: -1
=== End output ===
In the first invocation of zzz() array @aaa start out undefined,
while in the second @aaa starts out as a defined array with length
0. This is not what I expected at all! I haven't been able to
find this behavior written up anywhere, so is it possible it is a
bug? If not, can anyone explain what is happening, or point me to
an explanation?