Interesting. I have this thing about not doing 0 == blah because I think that reads better as !blah so I wouldn't have had the problem. In many C++ish languages the two are essentially the same so it's pretty much personal preference between an ugly verbose numeric compare and a clean succinct boolean test. In Perl, as you may have found, it can be more subtle than that!
That said, how were you performing your empty test? The simple version works fine for me:
use strict;
use warnings;
my %hash;
print "$^V\n";
print "Empty: !\%hash\n" if !%hash;
print "Empty: ! scalar\n" if ! scalar %hash;
print "Empty: 0 == scalar\n" if 0 == scalar %hash;
print "Empty: ! keys\n" if ! keys %hash;
++$hash{entry};
print "Not empty: \%hash\n" if %hash;
print "Not empty: scalar\n" if scalar %hash;
print "Not empty: 0 != scalar\n" if 0 != scalar %hash;
print "Not empty: keys\n" if keys %hash;
Prints:
v5.32.1
Empty: !%hash
Empty: ! scalar
Empty: 0 == scalar
Empty: ! keys
Not empty: %hash
Not empty: scalar
Not empty: 0 != scalar
Not empty: keys
Update: fixed mismatch between test and print for 0 == scalar
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|