Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2023-09-28 16:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found