Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Coding styles using if/else

by blazar (Canon)
on Apr 24, 2007 at 11:27 UTC ( [id://611696]=note: print w/replies, xml ) Need Help??


in reply to Re: Coding styles using if/else
in thread Coding styles using if/else

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://611696]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-20 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found