in reply to Re^2: Ways to control a map() operation
in thread Ways to control a map() operation

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 :)

Replies are listed 'Best First'.
Re^4: Ways to control a map() operation
by Leviathan (Scribe) on Jul 16, 2006 at 11:41 UTC
    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.
      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.

      That's what I said, isn't it?

        You're playing on words now. The original point is you cannot leave an eval with last. Otherwise this should work:

        for (1..100) { eval { if ($_ % 2 == 0) { last; } print $_, "\n"; }; }

        and expect it to print only odd numbers.

        --
        Leviathan.