in reply to Re: Constant value in anonymous sub
in thread Constant value in anonymous sub

Sorry I disagree that this is a good example - "by the book" - of a closure, because it's far to magic.

Each closure has a new instance of $i which is mutable, looking at the code I would expect either always the same instance of $i or $i being an alias of an immutable literal... but it's neither of these...

for my $i (1..3) { $numbers{$i} = sub { $i++;print $i }; }

Cheers Rolf

UPDATE:

OK my problem derived from the difference between range operator and comma operator in producing lists:

DB<37> for my $i (1,2,3) { $numbers{$i} = sub { $i++;print $i }; pri +nt \$i} SCALAR(0x9338210)SCALAR(0x92bdc90)SCALAR(0x9338190) DB<38> &{$numbers{1}} Modification of a read-only value attempted at (eval 43)[/usr/share/pe +rl/5.10/perl5db.pl:638] line 2.

Replies are listed 'Best First'.
Re^3: Constant value in anonymous sub (closures & aliases)
by doug (Pilgrim) on Feb 19, 2010 at 15:57 UTC
    Sorry I disagree that this is a good example - "by the book" - of a closure, because it's far to magic.
    Each closure has a new instance of $i which is mutable, looking at the code I would expect either always the same instance of $i or $i being an alias of an immutable literal... but it's neither of these...

    Rolf, $i is a variable and it is always mutable. At the end of the closure (the loop) it goes out of scope, so there is no way to reference it directly. There are still references to $i in the subroutines that were generated inside the loop/closure. Via these subroutines you can do whatever you like to $i, including returning it (as reference, of course) and manipulating it outside of a subroutine.

    Basically leaving a block only removes the symbol table entry for something. The object itself doesn't cease to exist until all references to it go away. This is very much by the book for Perl.

    - doug

      > Rolf, $i is a variable and it is always mutable.

      Doug, plz have a second look at the code (incl. errormsg) at the end of the post ... 8)

      Cheers Rolf

      UPDATE: Maybe it wasn't clearly enough expressed ...

      for my $i ( LIST ) produces _aliases_ to the listelements (even to read-only values)!

      It's NOT trivial (I would even say dangerous) when the explanation of closures has this extra complication, so it shouldn't be in any book.

        My point was simply that it was a good by the book example of a closure. I stand by that statement.

        Your issue seems to be more with how foreach makes its iterator variable an alias to the underlying object. I take it that you think it should be a copy, and not an alias? Well, that isn't very perl4-ish, and foreach has a long history. Just sticking with modern stuff, consider

        foreach my $i ( @list ) { $i =~ s/foo/bar/g; } go_do_something(@list);

        Would you expect the go_do_something() routine to get the unmodified values of @list, or would you expect the s/foo/bar/g transformation to have occured?