Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Closure Confusion

by kabel (Chaplain)
on Oct 16, 2002 at 21:04 UTC ( #205846=note: print w/replies, xml ) Need Help??

in reply to Closure Confusion

i tried another way and confused myself somehow ;)

my initial thought was that if you put the scalar value inside double quotes it immediately stringifies, thus evaluating to a string that is just printed out.
push (@closures, sub {print "$j";});
this does not happen - it seems that the sub call takes the block as-is and returns a code reference. is there an explicit stringify operator for scalars? is this way feasible? my feeling says no, but i do not know it.

Replies are listed 'Best First'.
Re^2: Closure Confusion
by Aristotle (Chancellor) on Oct 17, 2002 at 01:54 UTC
    Don't confuse creating a closure with compilation. The closure's body is compiled at compile time, not when the closure gets created. The code of this closure says "stringify $j", but which $j is only decided at runtime. Creating a closure at runtime does not compile the code; it merely "connects" it with a specific instance of whichever lexicals it refers to. Since you only create a single $j, all your closures get "connected" to the same $j.

    Makeshifts last the longest.

      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 ;)
        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.

Re: Re: Closure Confusion
by chromatic (Archbishop) on Oct 16, 2002 at 23:28 UTC

    Declaring a closure is much like declaring a subroutine. It works just as you'd expect this code to work. (Of course, if you don't expect this code to work the way it works, we all have problems :):

    sub not_a_closure { my $j = shift; print "$j"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://205846]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-06-21 14:39 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.