P
Paul Lalli
Greetings.
I recently found out entirely by accident that it is possible to
localize members of a hash or array which have been declared lexical.
In other words, the following "works" just fine:
#!/usr/bin/perl
use strict;
use warnings;
my @n = (1..10);
print "N: @n\n";
{
local $n[5] = 'foo bar';
print "N: @n\n";
}
print "N: @n\n";
__END__
N: 1 2 3 4 5 6 7 8 9 10
N: 1 2 3 4 5 foo bar 7 8 9 10
N: 1 2 3 4 5 6 7 8 9 10
I discovered this when I wanted to temporarily change an attribute of a
database handler reference in a large chunk of code. After compiling
and running, I suddenly realized "Wait, that shouldn't work.". I was
shocked to find that it did.
My shock stems from the long-held (apparently wrong) belief that only
package variables can be localized. And indeed, it seems that is
partially true - an entire lexical variable cannot be localized, only
members of lexical aggregates:
#!/usr/bin/perl
use strict;
use warnings;
my @n = (1..10);
print "N: @n\n";
{
local @n = (11..20)
print "N: @n\n";
}
print "N: @n\n";
__END__
Can't localize lexical variable @n at ./lex.pl line 8.
Reading up on local in perlsub, I find that my findings do match what
the docs say, but that the docs are rather vague on the topic as a
whole. They say that local may only localize Package Variables, but in
a later paragraph, may also localize "certain l-values", such as parts
of hashes and arrays.
All of this boils down to a single question: If members of lexical
hashes and arrays can be localized, is there any particular *reason*
for the inability to localize entire lexical variables?
Thanks for your input,
Paul Lalli
I recently found out entirely by accident that it is possible to
localize members of a hash or array which have been declared lexical.
In other words, the following "works" just fine:
#!/usr/bin/perl
use strict;
use warnings;
my @n = (1..10);
print "N: @n\n";
{
local $n[5] = 'foo bar';
print "N: @n\n";
}
print "N: @n\n";
__END__
N: 1 2 3 4 5 6 7 8 9 10
N: 1 2 3 4 5 foo bar 7 8 9 10
N: 1 2 3 4 5 6 7 8 9 10
I discovered this when I wanted to temporarily change an attribute of a
database handler reference in a large chunk of code. After compiling
and running, I suddenly realized "Wait, that shouldn't work.". I was
shocked to find that it did.
My shock stems from the long-held (apparently wrong) belief that only
package variables can be localized. And indeed, it seems that is
partially true - an entire lexical variable cannot be localized, only
members of lexical aggregates:
#!/usr/bin/perl
use strict;
use warnings;
my @n = (1..10);
print "N: @n\n";
{
local @n = (11..20)
print "N: @n\n";
}
print "N: @n\n";
__END__
Can't localize lexical variable @n at ./lex.pl line 8.
Reading up on local in perlsub, I find that my findings do match what
the docs say, but that the docs are rather vague on the topic as a
whole. They say that local may only localize Package Variables, but in
a later paragraph, may also localize "certain l-values", such as parts
of hashes and arrays.
All of this boils down to a single question: If members of lexical
hashes and arrays can be localized, is there any particular *reason*
for the inability to localize entire lexical variables?
Thanks for your input,
Paul Lalli