in reply to Ways to control a map() operation

last cannot be used to exit a block which returns a value such as eval {} , sub {} or do {}
This is a bit ambigous. As a matter of fact, you can leave eval{}, sub{} or do{} using last. Have a look at this code:
sub foo { last; } for (1..3) { print; do { eval { foo (); }; }; }
It will print only "1". This is because the "last" will jump out of the for-loop, regardless of the surrounding sub{}, eval{} and do{}.

Replies are listed 'Best First'.
Re^2: Ways to control a map() operation
by Leviathan (Scribe) on Jul 16, 2006 at 07:31 UTC

    You really cannot leave eval{} with a last.

    last will exit a loop block, and eval {} is not a loop block. Your example works here because you're cheating :) you placed a loop block around the eval, which is what last is exiting, and not the eval itself.

    Compare your version with

    eval { print "1\n"; last; print "2\n"; };

    Seems to work you say? Take a closer look.

    eval { print "1\n"; last; print "2\n"; }; print "Oops: $@\n" if $@;

    Aha!!! the only reason we exit from the eval is because we are dying, and not because last allows you to exit eval. You may argue that it's exiting in either way, but that's not a good solution since you lose the advantages of eval that way.

    --
    Leviathan.
      You really cannot leave eval{} with a last.
      Oh yes, you can. But you will not only leave the eval but the loop around it, too. If there is no loop around that eval, you'll die—just like your code demonstrated.
      eval { last; }; warn $@ || 'no error'; for (1) { eval { last; }; } warn $@ || 'no error';
      The output of the above code is:
      Can't "last" outside a loop block at x.pl line 2. no error at x.pl line 11.
      By the way, even if the code in my previous post had died in the eval{}, the loop would have continued and the output would have been 123.

      (++ for you anyway, because you almost tricked me into admitting that you're right :)

        Oh yes, you can. But you will not only leave the eval but the loop around it, too. If there is no loop around that eval, you'll die—just like your code demonstrated.

        No. You don't leave the eval, you just evaluate a last that will make you leave the loop.

        even if the code in my previous post had died in the eval{}, the loop would have continued and the output would have been 123

        Of course, but it will not die in the eval, because eval { last; } is like saying last; -- i.e: you're just evaluating the code

        eval { bleh(); } is really like saying just bleh(); so your code is simply:

        for (1) { last; }

        Otherwise, if it already exits the eval block, why does it exit the loop too?

        so just like saying last; outside a loop will die, saying it inside an eval that is outside a loop will die too. It will exit the eval, but just because it died, not because it skipped the block.

        --
        Leviathan.
Re^2: Ways to control a map() operation
by shmem (Chancellor) on Jul 16, 2006 at 12:58 UTC
    This is a bit ambigous. As a matter of fact, you can leave eval{}, sub{} or do{} using last.

    This is plain wrong. please wait for my update before answering.. thanks

    perl -le 'sub leave { last }; leave(1)' Can't "last" outside a loop block at -e line 1. perl -le '$foo = do { last }' Can't "last" outside a loop block at -e line 1. perl -le '$foo = eval { last if 1; 2; }; print $@ if $@; print ">$foo< +"' Can't "last" outside a loop block at -e line 1. ><
    Rather, you can combine eval, sub and last in some obscure way which will look like it's right and perl didn't complain (although it does).

    update: last exits the loop which is around such blocks immediately, thus works like a goto and is likely to leave a mess behind.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}