$foo = 5;
sub do_something { print $foo, $/; }
sub main
{
do_something();
{
local $foo = 6;
do_something();
}
do_something();
}
main();
The above code is a typical use of local. Internally, it is conceptually equivalent to the following code (shown for the innermost block only). You'd get 5, 6, 5 printed.
{
my $unnamed_backup_of_old_value_of_foo = $foo;
$foo = 6;
do_something();
$foo = $unnamed_backup_of_old_value_of_foo;
}
The actual storage of $foo is the first statement. It is what gets changed whenever you see assignments being done.
This of course can get very big and hairy with a huge hash of data to "backup" and "restore" in the equivalent %unnamed_backup_of_old_value_of_datahash. Also, deleting items from the local hash would not have much bearing, since the whole hash will just get tossed out and restored from the backup.
If you're doing local from multiple threads, well, you can see where your memory is going. Secondly, I can quite easily imagine a race condition where your main shared global hash is getting "restored" in the wrong order, leading to pandemonium.
-- [ e d @ h a l l e y . c c ]
|