in reply to Re^2: Lost anonymous subs
in thread Lost anonymous subs

Well yes, that's because the = operator copies stuff. There's nothing special about that.

Replies are listed 'Best First'.
Re^4: Lost anonymous subs
by kappa (Chaplain) on Dec 10, 2004 at 12:07 UTC
    my @abc; for my $m qw/a b c/ { push @abc, sub { $m }; } print $_->() . "\n" for @abc;
    No assignments and still, those subs each has its own $m. Or does the for my $m creates a new variable on each iteration?
    --kap

      The my() operator has a runtime effect of creating a new variable so each time you start that loop, you have a new variable to have potentially bound to. This also works if you write code like this.

      my @abc; for my $m ( qw/a b c/ ) { push @abc, \ $m; } print @abc
        I may sound annoying but what about this? The my is executed only once.
        my (@abc, $m); for $m ( qw/a b c/ ) { push @abc, \ $m; } print "$$_\n" for @abc;
        --kap