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

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.

Replies are listed 'Best First'.
Re: Re^4: Closure Confusion
by kabel (Chaplain) on Oct 17, 2002 at 14:32 UTC
    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 :|