Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by why_bird (Pilgrim)
on Mar 04, 2008 at 17:28 UTC ( [id://671948]=note: print w/replies, xml ) Need Help??


in reply to Re: 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* :)
........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......

Replies are listed 'Best First'.
Re^3: Exiting subroutine via next (that old chestnut)
by nefigah (Monk) on Mar 04, 2008 at 20:59 UTC
    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://671948]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-03-29 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found