Re: Can't localize lexical variable $var at...
by Elian (Parson) on Jun 09, 2002 at 18:02 UTC
|
The docs are pretty clear on this--you can only localize global variables.
This is partially for hysterical raisins, as global variables and local predated lexicals by quite a bit in perl's history. Local is a hackish way to implement lexical scope without lexical variables.
There's also little reason to use local with a lexical--since local overrides a variable until the end of the scope, you might as well either overwrite the lexical (if it's in the scope the lexical was declared) or my a new version of the lexical (if it's in an inner scope from the declaration). | [reply] |
|
Just for completeness, you can localize an element of a lexical hash or array. How useful this is, I don't know.
#!/usr/bin/perl
use warnings;
my %hash = (a=>1, b=>2);
{
local $hash{'a'} = 999;
print map {"$_ = $hash{$_}, "} sort keys %hash;
print "\n";
}
print map {"$_ = $hash{$_}, "} sort keys %hash;
print "\n";
I've always been a little confused as to why this was implemented for elements of a lexical hash or array but not scalar
-Lee
"To be civilized is to deny one's nature." | [reply] [d/l] |
|
That's one of the things that slipped in under the radar. There's no real difference between elements of a lexical and global array or hash, so you can localize either with no penalty. It's a quirk of the implementation--whether it's a bug or not is arguable. (local's already got a number of problems, mainly when localizing anything with magic)
| [reply] |
Re: Can't localize lexical variable $var at...
by Kanji (Parson) on Jun 09, 2002 at 18:07 UTC
|
You can find some additional enlightenment in Dominus' Coping with Scoping, but to get the same effect with a my-declared variable, you can just redeclare it in a different scope (ie, inside a { ... }).
perl -lwe "my $var='qq';{my $var=2;print $var;}print $var"
--k.
| [reply] [d/l] |
Re: Can't localize lexical variable $var at...
by ariels (Curate) on Jun 09, 2002 at 19:05 UTC
|
That's because...
...you can't localize a lexical variable. It doesn't make any sense.
A lexical variable has visibility limited by space: it is visible precisely within its scope, from the point of definition to the end of that block. It is known (except if shadowed) by its name throughout the scope, and it does not exist anywhere outside its scope. In particular, a sub outside the scope cannot see that variable.
Compare with a localized instance of a global variable. This has its visibility limited by time: it starts having its new value when local is executed, and continues with that value until execution of that block finishes. As such, to know its value you have to know about the particular execution path in effect. At different times, the same reference to a global variable may refer to totally different instances of it; a reference to a lexical variable always refers to the the same variable.
Consider a function (external to the scope) which uses a lexical <samp>$x</samp>. It always refers to the same <samp>$x</samp>, and calling code cannot change that. A function which uses a global <samp>$x</samp>, by contrast, refers to the current instance of <samp>$x</samp>; calling functions can change which instance that is.
Generally, global variables are useful for options. For instance, if your functions refer to a global variable $debug, you can turn on debugging for the entire program by saying "$debug=1" at the top. But you can also turn on debugging just for a small portion of the run by saying
{
local $debug = 1;
# Code here prints debugging messages
# ...
}
# Code here reverts to the old status
You can't do that (conveniently) with lexicals.
Mixing the 2 is (to mix a metaphor; sorry) mixing time and space; it just doesn't work.
| [reply] [d/l] [select] |
|
NETaa13502: now complains if you use local on a lexical variable
From: Larry Wall
Files patched: op.c
Now says something like
Can't localize lexical variable $var at ./try line 6.
I cannot find a rationale though - perhaps the p5p archives
have such a rationale, if they go back that far.
Abigail | [reply] [d/l] |
|
you wrote: In particular, a sub outside the scope cannot see that variable.
But subs inside that scope are able to see that! And I cleanly see a way how that lexically scoped variable could be stacked, just like global variable!
But yes, I'll better just use lexical variables and implement stacking if I really need.
Best wishes, Courage, the Cowardly Dog
| [reply] |
|
| [reply] [d/l] [select] |
|
|
|
(jeffa) Re: Can't localize lexical variable $var at...
by jeffa (Bishop) on Jun 09, 2002 at 19:12 UTC
|
local is baad,
m'kay?
Seriously, as the others have pointed out, you don't need
it. Consider our instead:
perl -lwe "my $var='qq';{our $var=2;print $var;}print $var"
or
perl -lwe "our $var='qq';{my $var=2;print $var;}print $var"
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
| [reply] [d/l] [select] |
Re: Can't localize lexical variable $var at...
by Aristotle (Chancellor) on Jun 10, 2002 at 13:57 UTC
|
You know. Maybe what you're looking for is this?
$ perl -lwe 'my $var="qq";{my $var=2;print $var;}print $var'
2
qq
____________ Makeshifts last the longest. | [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |