http://qs321.pair.com?node_id=11135639


in reply to How to test for empty hash?

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