Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: "return" to break out of a loop

by ikegami (Patriarch)
on May 13, 2008 at 14:30 UTC ( [id://686275]=note: print w/replies, xml ) Need Help??


in reply to "return" to break out of a loop

sub foo { for (...) { return 1 if ...; } return 0; }
is actually a rather common construct. If you don't like it, you could use a flag variable.
sub foo { my $found = 0; for (...) { if (...) { $found = 1; last; } } return $found; }
You could even get rid of the last by changing the loop control. But everytime you change something from the original, it gets bigger and more complex, and therefore harder to read and more error prone.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://686275]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-19 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found