Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: YAlQ: Yet Another local() Question.

by jlistf (Monk)
on Aug 03, 2000 at 18:02 UTC ( [id://25972]=note: print w/replies, xml ) Need Help??


in reply to YAlQ: Yet Another local() Question.

here's something you missed:
my provides deep binding. this means that you can create closures with my. the variable will take on the value that existed when the closure was created.
local provides shallow binding. within a closure, local will take on a value dependant on when the subroutine is called.

sub add($); sub ladd($); my $addOne = add(1); my $laddOne = ladd(1); &$addOne(0); &$addOne(1); &$laddOne(0); &$laddOne(1); sub add($) { my $add = $_[ 0 ]; return sub { my $num = $_[ 0 ]; my $sum = $num + $add; print "$num + $add = $sum\n"; }; } sub ladd($) { local $add = $_[ 0 ]; return sub { local $num = $_[ 0 ]; local $sum = $num + $add; print "$num + $add = $sum\n"; }; }
returns:
0 + 1 = 1 1 + 1 = 2 0 + = 0 1 + = 1
jeff

Replies are listed 'Best First'.
RE: Re: YAlQ: Yet Another local() Question.
by BlaisePascal (Monk) on Aug 03, 2000 at 18:26 UTC
    Yes? How is that inconsistant with what I said? $laddOne refers to $add as a global variable. $add has no lexical definition in scope of its use in $laddOne, so it'll use the current global value.

    Granted, when the anonymous subroutine that is referenced by $laddOne was created, the global value of $add was 1, but so what? That value isn't evaluated within the -dynamic- scope of the local.

    I still don't see what I'm missing. Maybe I just came into the game late, and didn't have all the baggage associated with local() in my mind before the introduction of my(). I see local as "mask global variable" not as "create local variable". Your "counterexample" doesn't show where I'm wrong.

      i was just emphasizing the fact that my provides deep binding and local provides shallow binding. it wasn't clear (to me) from your post that you knew this.

      jeff

      Update: when you implement the transformation, the closure works again. apparently the transformation you propose doesn't actually work. or is that your point? that local provides no binding at all, but transforming it into an implementation with my does provide binding. whatever the case, local's binding is called "shallow binding" in the documentation, so thats what i'll call it here.
        OK, I see.

        Well, if I rewrite your ladd based on the transform I proposed, I get:

        sub ladd($) { my $hidden_add = $add; $add = $_[ 0 ]; return sub { my $hidden_num = $num; $num = $_[ 0 ]; my $hidden_sum = $sum $sum = $num + $add; print "$num + $add = $sum\n"; ($num,$sum) = ($hidden_num,$hidden_sum); }; $add = $hidden_add; }
        All the variables have global lexical scope except the hidden ones, which are never directly used. I don't see local providing any "binding" at all.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://25972]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found