Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Use of 'local'

Hi,

It seems that the 'local' keyword isn't used much in perl 5. I just wanted to double-check if my understanding of it is correct.

When you declare a local variable, it replaces the value of a global variable of the same name, until the scope in which is was used is exited, and then it will return the variable back to its global value.

Example:

our $g = 5;

sub a()
{
    local $g = 10;
}

sub b()
{
    print($g, "\n");
}

print($g, "\n");
a();
print($g, "\n");

So the above is going to print:

   5
   10
   5

That's because g, a global, is set to 5 initially. Inside a(), it is re-scoped as a local. So the global value of $g is 'put on the side' temporarily. Now anytime I use $g inside a() or any scope called within a(), it's going to appear as if $g is 10?

Finally when I exit the scope of a(), $g is restored to its original value of 5.

Is that right? It seems like a confusing method.

Thanks
Avatar of ozo
ozo
Flag of United States of America image

well, the above program doesn't print the 10, but you're right about it printing 5
see
perldoc -f local
local was implemented long before my was created, so it was sort of a stopgap
but it still has use for built in variables
(you can't do my $/; )
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Shoot, sorry, you're right, I forgot the call to b(). Please ignore the first post then, this is what I meant for it to be:




our $g = 5;

sub a()
{
    local $g = 10;
    b();
}

sub b()
{
    print($g, "\n");
}


print($g, "\n");
a();
print($g, "\n");


>> The call to sub a doesn't change the value of the global $g
This is what I don't get, so what is the perl interpreter actually doing? Is it making a new variable $g, which is only visible within a() and subs it calls? Therefore since a() calls b(), it only sees the 'local' instance of $g. The global $g never changed.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
see
Temporary Values via local()
in
perldoc perlsub
That may be a bit over simplified, but it can still be a useful rule of thumb for beginners until they learn enough to know when it's safe to ignore it.
e.g.
{ local $/ = undef; $slurp = <FILE>; }


see also
perldoc -q "What's the difference between dynamic and lexical \(static\) scoping"
Ok from your links, these two definitions seem to agree:

local($x) saves away the old value of the global variable $x, and assigns a new value for the duration of the subroutine, which is visible in other functions called from that subroutine. This is done at run-time, so is called dynamic scoping. local() always affects global variables, also called package variables or dynamic variables.

or

First, here's what local $x really does: It saves the current value of the package variable $x in a safe place, and replaces it with a new value, or with undef if no new value was specified. It also arranges for the old value to be restored when control leaves the current block. The variables that it affects are package variables, which get local values. But package variables are always global, and a local package variable is no exception.


So in my own words, when you use local on a global var (you can only use it on a global var), perl puts the original value of the global in a magical place and replaces its value with whatever you set (or undefs it if you didn't set it). Any calls at the scope in which you local'd it, or scopes called from there, will see the replaced global variable, until the scope in which you local'd it exits, then perl replaces it with the value tucked away in the magical place.

I can see how this would be useful without the 'my' keyword.
yes