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

Re: When warnings get in the way

by halley (Prior)
on Apr 22, 2005 at 16:59 UTC ( [id://450489]=note: print w/replies, xml ) Need Help??


in reply to When warnings get in the way

This isn't in direct answer to your question, but may help you to code in a "more Perlish" way, and have to work less to get the answers you want.

The traditional value for "falsehood" in Perl is undef. When converted to a string, that looks like "", and when converted to a number, that looks like 0, and when converted to a list, that looks like (); all three are also considered false. The special string "0" is also considered false, because it would convert to a false value if treated as a number. These are the only values considered false, by the way.

The traditional value for "truth" in Perl is 1. Actually, anything that is not false is considered true, so your string "true" is suitable, but perhaps overkill and misleading too.

Coding to expect truth or false by comparing against a specific value may mislead your readers. Use specific values when they're important, but use 1/undef when you just want to keep a boolean flag value. Code your conditionals to just look at that flag, and not compare that flag to some specific value. Getting in that habit will help you avoid introducing errors when the variable is true but not equal to "true" for example.

Consider coding this way:

$foo = 1; if ($foo) { print "Yes, it's true.\n"; } if (not $foo) { print "No, it's false.\n"; } $foo = undef; if ($foo) { print "Yes, it's true.\n"; } if (not $foo) { print "No, it's false.\n"; }

--
[ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

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

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

    No recent polls found