I don't use
exists $hashtest{abc} and defined $hashtest{abc} because
defined can handle
undef. However, sometimes I do
defined $hashtest{abc} and $hashtest{abc} because I want the
false condition to be based on more than
$hashtest{abc} being
undef. I also want it to evaluate to
false if it's an empty string or
0, in addition to
undef. In that case because the second condition is not protected by
defined I need to make sure that the key exists. TBH I don't remember the last time I even used
exists.
To summarize:
# generally pointless, I would not use the mere existence of the key t
+o mean anything; check it's value
if ( exists $hashtest{abc} and defined $hashtest{abc} ) {
print qq{This is worthless IMO.\n};
}
Rather just do this if
undef is taken as
does not exist.
if (defined $hashtest{abc}) {
print qq{Better than 'exists'; check for the value being undef or no
+t undef.\n};
}
And if I want to check the value, something like:
if (defined $hashtest{abc} and $hashtest{abc}) {
print qq{Also, better than 'exists' and when I want to make sure the
+ value is not "falsey"\n};
}
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.