in reply to Re: local variable syntax
in thread local variable syntax
In summary, you basically never want to use local.
local is widely misunderstood. The best evidence of this is the frequency of statements like "never use local."
It's more accurate and useful to say that local operates on all variables... except lexical (my) variables. It isn't just for localizing
values of whatever package globals your program may have, it is
essential for the special globals like $_ and @ARGV.
Furthermore — and this useful fact is often missed — it can localize values in variable spaces which are automatically allocated by the interpreter — for example, elements of arrays and hashes.
use Data::Dumper; use strict; use warnings; my %h = ( foo => 1 ); { local $h{'foo'} = 2; print Dumper \%h; } print Dumper \%h;
$VAR1 = { 'foo' => 2 }; $VAR1 = { 'foo' => 1 };
I would summarize by saying that As different as lexical variables are from package variables, so is local from my.
local is a run-time function, whereas my is a compile-time keyword (with some run-time effects).
local is very useful in its own way. It is by no means a drop-in replacement for my
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: local variable syntax
by fishbot_v2 (Chaplain) on Apr 27, 2006 at 14:31 UTC | |
by jdporter (Paladin) on Apr 07, 2011 at 16:54 UTC |