Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

RE: Re: YAlQ: Yet Another local() Question.

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


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

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.

Replies are listed 'Best First'.
RE: RE: Re: YAlQ: Yet Another local() Question.
by jlistf (Monk) on Aug 03, 2000 at 18:38 UTC
    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://25976]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-23 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found