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

Hi all

This post is vaguely relevant to what I want to ask. As far as I can tell, the problem with exiting from a sub via next is that it performs the next iteration of any loop from within which it was called. However, that's the intention of my subroutine:

use strict; use warnings; sub RemoveComments { my $line=$_[0]; if ($$line =~ /#/){ $$line =~ s/#.*$//; } if ($$line =~ /^\s*$/){ next; } } .... while ($temp=<INPUT>){ &RemoveComments(\$temp); ... some other code ... }

This seems to happily remove commented lines from the file I'm reading in (assuming comments are perl like) and then throws away the line if the whole line is empty (i.e. comments at the start of a line, or competely blank lines) by calling the next iteration of the while $temp=<INPUT> loop. Otherwise it passes the de-commented line through the rest of the script.

However, although it appears to work happily, it throws an Exiting subroutine via next at line yy, <INPUT> line zz. error (and there's quite a lot of those so it's quite annoying!)

The only other way I could see to do it would be to get &RemoveComments() to return a variable, say '$empty', and then have the main code evaluate this and decide whether to do a 'next' or not:

use strict; use warnings; sub RemoveComments { my $line=$_[0]; if ($$line =~ /#/){ $$line =~ s/#.*$//; } if ($$line =~ /^\s*$/){ return 1; } } .... while ($temp=<INPUT>){ my $empty =0; $empty=&RemoveComments(\$temp); if ($empty == 1){ next; } ... some other code ... }

which just seems more messy to me, and more annoying to have to write an extra if(){} in every place that I just had to write &RemoveComments before. a)are my statements correct and/or b) am I missing a better way to do this or c) is my code happily working away nicely by accident..?

thanks :)
Those are my principles. If you don't like them I have others.
-- Groucho Marx