Click to See Complete Forum and Search --> : keyword local


superstition
04-30-2005, 10:09 PM
ex:
$tt=0;
sub i()
{
local $tt=1;
j();
}
sub j()
{
print \$tt,"\n";
}

i();
j();

why addresses are different between i() called and j() called ??

function i virtual code:

sub i()
{
#oldvalue is push to stack.
$oldvalue=$tt ; # Is the variable $tt in function i equal to global $tt ??
$tt=1;
j();
$tt=$oldvalue; #restor
}

Is therer something wrong witn my idea of function i virtual code ??

Jeff Mott
05-01-2005, 02:06 AM
why addresses are different between i() called and j() called ??Because the call to i() creates a new temporary $tt variable that is visible to the print statement when i() calls j().function i virtual code:

sub i()
{
#oldvalue is push to stack.
$oldvalue=$tt ; # Is the variable $tt in function i equal to global $tt ??
$tt=1;
j();
$tt=$oldvalue; #restor
}

Is therer something wrong witn my idea of function i virtual code ??No, there is nothing wrong. Though the code could be cleaner and more structured if you just passed the value as a parameter instead.sub i
{
$tt = 1;
j($tt);
}

superstition
05-01-2005, 04:51 AM
Because the call to i() creates a new temporary $tt variable that is visible to the print statement when i() calls j().No, there is nothing wrong.

No,there is nothing wrong. The expression represents virtual code is right.

why new temporary $tt is produced in function i ??

The variable $tt in funition i should be the global $tt.

Please you explain the situation clearly.

The virtual code is picked from "Program Perl,3e".

{
local $var=$newvalue;
some_func();
...
}
==>
{
$oldvalue=$var;
$var=$newvalue;
some_func();
...
}
continue
{
$oldvalue=$var
}

The new temporary $tt is the $var or $oldvalue ??

My english is not good.

superstition
05-01-2005, 05:47 AM
I found some information about the situation.

Perl is made from language c.

C: the structure of scalar value.

truct STRUCT_SV { /* struct sv { */
void* sv_any; /* pointer to something */
U32 sv_refcnt; /* how many references to us */
U32 sv_flags; /* what we are */
};

The address of \$tt is the sv_any.

When you use keyword local,compiler will produce a new pointer of sv_any

for efficiency.

Do you mean that the new temporary $tt is the new pointer of sv_any ?