Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
foreach my $foo (@bars) { { if ($foo =~ /baz/i) { last unless $foo eq uc $foo; } else { last unless $foo ne uc $foo; } # stuff } do_more_stuff($foo); }
works just fine. There are several other options as well. First there is the continue that you mentioned:
foreach my $foo (@foos) { if ($foo =~ /baz/i) { next unless $foo eq uc $foo; } else { next unless $foo ne uc $foo; } # stuff } continue { do_more_stuff($foo); }
and then (depending on what you do in the if statement) there is reversing the order of your operations:
foreach my $foo (@foos) { do_more_stuff($foo); if ($foo =~ /baz/i) { next unless $foo eq uc $foo; } else { next unless $foo ne uc $foo; } # stuff }
You can have the chain of ifs be replaced by a function call that returns from multiple points:
foreach my $foo (@foos) { do_stuff($foo); do_more_stuff($foo); } sub do_stuff { my $foo = shift; if ($foo =~ /baz/i) { return unless $foo eq uc $foo; } else { return unless $foo ne uc $foo; } # stuff }
And so on.

I have, in fact, used every one of these solutions except the goto one, and I prefer all of them to the goto solution. Why? Because I find each of them clearer, they allow me to see program flow in terms of following blocks. The goto forces me to see an element of program flow which is not some form of block. I don't like that. Furthermore, no matter how clearly the goto solution's intention may be, its use opens up the possibility of traps like this:

foreach (1..5) { print "Hello\n"; goto DONE; } DONE: foreach (1..5) { print "World\n"; goto DONE; } DONE:
By contrast anything only relying on return and loop control statements can be much more easily verified correct based on local examination. (OK, in this case your goto construct is verifiably correct without looking at outside code. But it will take more time for most people to figure out why the one is verifiably OK while the other is demonstrably bad than it will to figure out my alternate solution.)

Again, this is not something I would use a goto for.

UPDATE
Fixed a thinko danger pointed out to me. last and next are not the same.


In reply to Re (tilly) 6: Fear of Large Languages (was: "Would you use goto") by tilly
in thread Would you use 'goto' here? by Ovid

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found