in reply to Desparate behavior in loop labels

use warnings; use strict; quit: for (1 .. 10) { check_early ($_); } sub check_early { my $value = shift; last quit if $value == 5; print "$value\n"; }

Prints:

1 Exiting subroutine via last at noname1.pl line 11. 2 3 4 Exiting subroutine via last at noname1.pl line 11.

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Desparate behavior in loop labels
by b4swine (Pilgrim) on Aug 29, 2007 at 03:07 UTC
    Thanks for the example.

    BTW it was very puzzling, until I replaced the print with a print STDERR which then prints immediately. Otherwise the 'Exiting...' message before line 2 is very confusing.

Re^2: Desparate behavior in loop labels
by amarquis (Curate) on Aug 29, 2007 at 12:59 UTC

    Just to make sure that I understand what is going on:

    The "last LABEL" (in this case "quit") looks around locally for the label, and doesn't find it. It then jumps back through the stack to look for the label, and drops a warning when it does so?

    I'd no idea that was possible. It sounds a little wonky, honestly, but I guess that's why it tosses a warning.

      I think you've hit the nail on the head there.

      Contorted control flow can sometimes be handy, but as it can lead to a maintenance nightmare it deserves a warning.

      Perhaps more importantly, there's probably some history to why last works that way in Perl, too. Raising the warning and providing the former functionality rather than breaking the functionality is a good compromise for features that have been declared as questionable practice.

      Update: s/questionably/questionable/; to clarify that last sentence a bit.