www.webdeveloper.com
Results 1 to 4 of 4

Thread: keyword local

  1. #1
    Join Date
    Apr 2005
    Posts
    3

    keyword local

    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 ??

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    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.
    Code:
    sub i
    {
    	$tt = 1;
    	j($tt);
    }
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Apr 2005
    Posts
    3
    Quote Originally Posted by Jeff Mott
    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.
    Last edited by superstition; 05-01-2005 at 03:59 AM.

  4. #4
    Join Date
    Apr 2005
    Posts
    3
    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 ?
    Last edited by superstition; 05-01-2005 at 05:06 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles