nop has asked for the wisdom of the Perl Monks concerning the following question:

Does it cause perl any problems to return from a subroutine from the middle of a loop?
sub blah { ... foreach my $b (@blah) { foreach my $y (@yuck) { ... if ($something) {return;} ... } } }
I seem to recall (from long ago) this was ill-advised (?) in some languages (Pascal?)

Replies are listed 'Best First'.
(jcwren) Re: returning from the middle of a loop
by jcwren (Prior) on Nov 06, 2000 at 08:43 UTC
    Not at all. Any locally scoped variables will evaporate, and you're golden.

    --Chris

    e-mail jcwren
Re: returning from the middle of a loop
by spaz (Pilgrim) on Nov 06, 2000 at 11:10 UTC

    One thing to remember is it may be advisible to do a little
    clean up work, for example closing file handles. Granted Perl
    will do this for you, but it's better to be safe.

RE: returning from the middle of a loop
by japhy (Canon) on Nov 06, 2000 at 17:34 UTC
    This is fine. What MIGHT be ill-advised, or at least might be ugly to look at in code, is a subroutine called in a loop that calls last() or next().

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
Re: returning from the middle of a loop
by Fastolfe (Vicar) on Nov 06, 2000 at 20:10 UTC
    As other posters said, this is fine. An alternative would be to do this:
    sub blah { ... OUTER: foreach my $b (@blah) { foreach my $y (@yuck) { ... last OUTER if $something; ... } } # clean up }