in reply to Exiting subroutine via next -- Again

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