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

Re^2: Local for lexicals (local $_[...])

by tye (Sage)
on Aug 10, 2009 at 18:10 UTC ( [id://787382]=note: print w/replies, xml ) Need Help??


in reply to Re: Local for lexicals
in thread Local for lexicals

sub lambda { my $f = $_[1]; for my $x ( $_[0] ) { return sub { return localize( $x, $_[0], $f ); } } } sub localize { my( $lexical, $val, $f )= @_; local( $_[0] )= $val; $f->(); }

(Updated order of arguments for no particularly good reason.)

Update: This, however, doesn't work (despite not generating an error). This is because (I believe) local localizes $_[0] by replacing the alias with a new SV that gets set to the new value. The prior value is restored by restoring the alias back into that spot in the array. Thus, the alias is never modified and thus the value of the lexical is never changed.

- tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found