Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Coding styles using if/else

by shigetsu (Hermit)
on Apr 05, 2007 at 22:30 UTC ( [id://608559]=note: print w/replies, xml ) Need Help??


in reply to Coding styles using if/else

Personally, I think it could be somehow a pathological case such as an implicit return from a sub, assuming you're just trying to avoid having following code executed; the exit; statement is fine though, if you explicitly want to exit. In former case, it works well, as long as you don't change anything - but, all of a sudden, it may break if you do (possibly triggered by removing/commenting the exit; statement accidentally).

As a side note:

If you're worried about the total size of the if {} else {} clause, you could give the ternary conditional operator (?:) a try (assuming your code doesn't continue below the ternary operator statement in this peculiar case):

($name eq "") ? print q{Sorry, name can't be empty} : print "Thank you, $name. Your submission is complete.";

Replies are listed 'Best First'.
Re^2: Coding styles using if/else
by blazar (Canon) on Apr 24, 2007 at 11:27 UTC
    If you're worried about the total size of the if {} else {} clause, you could give the ternary conditional operator (?:) a try (assuming your code doesn't continue below the ternary operator statement in this peculiar case):
    ($name eq "") ? print q{Sorry, name can't be empty} : print "Thank you, $name. Your submission is complete.";

    (Sorry to reply so late but) this looks very wrong to me: the ternary operator is designed to operate on values, not to use it as a general purpose branching tool for its side effects. I would never do so except in golf: in particular the above example becomes perfectly fine if you amend it by factoring out the print:

    print +($name eq "") ? q{Sorry, name can't be empty} : "Thank you, $name. Your submission is complete.";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-23 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found