Trying to control a loop from a subroutine called from inside the loop is just asking for trouble. It might seem like it will work... it might even work in some circumstances... but most likely it's just going to fail. Can you see how Perl is confused by your instructions? Basically, you're trying to use a subroutine for loop control when you should be doing some form of nesting, and leave loop control inside the loop:

sub before_next_stuff { # lots o stuff } MAINJOB: foreach ( @array ) { if ( $something ) { # do stuff if ( $something_1 ) { # time to move to next iteration # of the loop before_next_stuff(); # you should only include this # loop control here if you must if ( $x > $stop ) { last MAINJOB; } elsif ( $y ne $text ) { foobar(); } else { next MAINJOB; } } } if ( $something_else ) { # do some other stuff before_next_stuff(); # you should only include this # loop control here if you must if ( $x > $stop ) { last MAINJOB; } elsif ( $y ne $text ) { foobar(); } else { next MAINJOB; } } # more stuff in loop before_next_stuff(); if ( $x > $stop ) { last MAINJOB; } elsif ( $y ne $text ) { foobar(); } # no "next" necessary here }


A big part of effective and elegant programming is to structure your loops so that controlling them is mostly a matter of natural flow. However, there are times when "natural flow" is simply impossible, and you need some way of interrupting loop flow -- which is why Perl has next and last functions. But they should still be used as sparingly as possible.

Bottom line: loop control should never be farmed out to a sub external to the loop. You can abstract everything else out into subs, but control needs to stay internal.

PS- Any time I have nested or complex loops, I use explicit labels (like "MAINJOB", above). They help both Perl and me (much more me than Perl) keep track of which loop is being controlled at any given time.


In reply to Re^3: next and last within subs by oakb
in thread next and last within subs by xorl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.