Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Re^2: Closure Confusion

by kabel (Chaplain)
on Oct 17, 2002 at 06:54 UTC ( #205939=note: print w/replies, xml ) Need Help??


in reply to Re^2: Closure Confusion
in thread Closure Confusion

thanks to you both for clarification. in fact that is the way things are going.

the real problem i think is that the code gets created at compile time. stringification cannot happen since the code has not run yet. so variables stay what they are (even with a hypothetical stringification operator).

the same is for the anonymous sub enclosed in an eval (). i could imagine that stringification then works (if only the operator would exist) ... but an eval is expensive, and more than one eval is even more expensive ;)

Replies are listed 'Best First'.
Re^4: Closure Confusion
by Aristotle (Chancellor) on Oct 17, 2002 at 12:54 UTC
    Oh, eval will let you do this: push @closures, eval "sub { print "\Q$j\E" }";

    (Note that if you don't quotemeta the variable, you open yourself up to all sorts of headaches - and a security hole if $j contains user input dependent data.)

    But why would you want that? Just create a unique copy of the lexicals in question during each loop iteration and you'll be fine. There's no reason to resort to eval STRING for this job. As all others have already proposed:

    my $my_j = $j; push @closures, sub { print $my_j };

    Makeshifts last the longest.

      i agree with the proposed solution. but thats not the point:

      the only thing i was interested in was: is it possible? yes, it is; but only with "runtime subroutine compilation".

      i think this attitude is ... err ... not that welcome here :|

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://205939]
help
Chatterbox?
[PriNet]: quick question... anyone know how to remove all the existing keys in a %hash ? i've tried several options, but when i re-use the hash, some of the "upper" keys that i don't re-use stick and i can't get them to "remove"

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2017-06-28 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (619 votes). Check out past polls.