Putting aside the problem for a moment, In your case, I think your code would become clearer and easier to maintain by not modifying @problems from a distance inside the subroutine. But feel free to change my example back to using global osmosis as a means of returning value from a subroutine if you think it is better in your case. (99% of the rant removed) :)

This example uses a hash of categories, where the categories are cuspid, isin, sedol, and ticker. You can iterate over each category and test it for compliance or problems.

Try this:

my @problems; foreach my $row ( @$data ) { my %categories; @categories{ cusip isin sedol ticker } = @$row; foreach my $category ( keys %categories ) { next if $category eq 'ticker'; if( not defined( $categories{$category} ) { push @problems, problem( $category, $categories{ticker} ); next; } } } sub problem { my ( $problem_tag, $ticker ) = @_; my $issue = "Missing $problem_tag for $ticker"; print $issue, "\n"; return $issue; }

In other words, if you have all that data to check, don't use individual variables, use a hash so that you can easily iterate over the keys (and not be tempted to resort to symbolic ref manipulation). The sub is defined outside the scope of your loop, and the next would have no idea where to next to. The loop isn't part of the caller stack. next has to come from inside the loop. Also, no need to use the &functionname notation in Perl5 under this circumstance. Try to get into the habit of not using it. Finally, wouldn't you agree it's better to not have a sub modifying a globally scoped variable when it can be avoided?


Dave


In reply to Re: Exiting subroutine via next -- Again by davido
in thread Exiting subroutine via next -- Again by BillyNg

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.