in reply to Re: Exiting subroutine via next
in thread Exiting subroutine via next

I don't really know how to explain ["next" cannot be used to exit a block which returns a value such as "eval {}", "sub {}" or "do {}"...]

It means next, last and redo ignore do blocks, eval blocks, etc. They will only cause loop blocks and bare blocks to repeat, exit, etc. It makes more sense when you read that same line in the documentation for last, since next doesn't cause a loop to exit.

foreach my $var (1, 2, 3, "skip", 4) { eval { # This block is ignored. if ($var eq "skip") { next; } }; print $var; } print("\n"); foreach my $var (1, 2, 3, "skip", 4) { { # This block exits. if ($var eq "skip") { next; } } print $var; } print("\n");

outputs

1234 123skip4

Replies are listed 'Best First'.
Re^3: Exiting subroutine via next
by geekphilosopher (Friar) on Dec 07, 2006 at 21:32 UTC
    Ah, that makes sense. Thanks!