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 xxx.pm 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
.......

In reply to Exiting subroutine via next (that old chestnut) by why_bird

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.