Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Exiting subroutine via next (that old chestnut)

by nefigah (Monk)
on Mar 04, 2008 at 20:59 UTC ( [id://672010]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Exiting subroutine via next (that old chestnut)
in thread Exiting subroutine via next (that old chestnut)

Thanks all, I'll change it, even if I don't like it as much. I can probably live with one or two more lines of code. I suppose there is a good reason for it throwing a warning at me.. *sigh* :)
It's good practice to never alter the control of a loop externally like that--it makes your subroutine very tightly coupled to that one specific usage, and in fact will blow things up if used differently. Functions should make it easier for you to reuse code in multiple contexts; it's one of their main reasons for existing. Most languages wouldn't even let you put a loop control variable in a sub like that (I know that's not entirely relevant, but just FYI) and Perl is reluctant to do so as well. Other people who may see your code in the future won't be expecting behavior like that.

I think the first reply's solution, using and, is an elegant alternative, and makes it possible to tell what is going on just by looking at the loop code.

My 2 cents!

Log In?
Username:
Password:

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

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

    No recent polls found