Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: whats wrong with this break ?

by wfsp (Abbot)
on Sep 17, 2010 at 13:34 UTC ( [id://860480]=note: print w/replies, xml ) Need Help??


in reply to whats wrong with this break ?

Try last (break out of a loop) instead of break (break out of a switch statement).

Replies are listed 'Best First'.
Re^2: whats wrong with this break ?
by Anonymous Monk on Sep 17, 2010 at 13:47 UTC
    excellent catch.... thanks.
      Have you considered using else instead of break?
      The break is going to make it harder to maintain, unless you heavily comment your code. Especially as this is not what you are actually doing with these lines but simplied to illistratrate your point, which will effective hide the break within your code.
      Using an else there instead will slow your script slightly for the additional check on every line, though the increase in maintainability might be well worth the cost.

        Actually I'd argue strongly in the other direction! The OP has two separate loops for handling different parts of a parsing process. At most a comment at the start of each is all that is required to make that clear. About the only change I'd make is to change the if statement into a statement modifier:

        last if /^2/;

        to make the last easy to see.

        Aside from making the code harder to maintain because you are conflating two different processing phases into one statement, using a needless else increases code nesting and makes it harder to follow code flow. I find early exits (such is indicated above) make the normal flow of code much easier to see.

        True laziness is hard work

Log In?
Username:
Password:

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

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

    No recent polls found