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

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

Replies are listed 'Best First'.
Re^6: Lost anonymous subs
by kappa (Chaplain) on Dec 10, 2004 at 15:27 UTC
    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

      Well... this gets into a feature of for() and stops being about my(). for is special in that it aliases the "a", "b", and "c" variables over $m during the loop so even though you didn't say for my the effect is the same. So there's a sort of implicit my() happening there anyway. What you'll notice though, is that if you used strict, the implicit my() won't take care of making sure that variable is declared so you'd still have an error unless you said it as for my or if the loop variable already existed outside the loop.

      At this point, however, you may just want to start reading the output of things like Devel::Peek::Dump, perl -MO=Concise your_script.pl, and reading pp_enteriter in pp_ctl.c.

        Gone there. Thanks.
        --kap