Perl tries to make things as easy as possible for you and this is a good example.
In most cases, what you actually want is as simple as:
if ($value) {
# $value contains something useful
} else {
# $value is "empty"
}
(Note that the logic is reversed from your original example)
What we're doing here is checking the "truth" of $value. $value will be "false" if it contains any of the following values:
- "undef" (i.e. if it hasn't been given a value)
- The number 0
- The string "0"
- The empty string ("")
Any other value in $value will evaluate to "true".
This is all you need in most cases. There are a couple of "corner cases" that you sometimes have to consider:
- Sometimes 0 is an acceptable value. You account for that by using if ($value or $value == 0).
- Sonetimes you might want to test that $value contains nothing but whitespace characters. The easiest way to do that is by checking for the presence of non-whitespace characters with if ($value =~ /\S/).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.