[Omaha.pm] print "%hash";

Jay Hannah jay at jays.net
Tue Oct 16 14:00:24 PDT 2007


I hit this code today

  $body .= "Email Address: ".$$results{email}."%0D%0A" if ($$results{email});

and thought "What? Isn't Perl going to try to interpret "%0D" as a hash?

Nope.

"%hash" is not interpreted at all. It's just a string.

Fun with hashes below...

j


$ cat j.pl
my %j = ( a => 1, b => 2 );
print %j, "\n";
print %j . "\n";
print "%j\n";

$ perl j.pl
a1b2
2/8
%j


perldoc perldata
   If you evaluate a hash in scalar context, it returns false
   if the hash is empty.  If there are any key/value pairs,
   it returns true; more precisely, the value returned is a
   string consisting of the number of used buckets and the
   number of allocated buckets, separated by a slash.  This
   is pretty much useful only to find out whether Perl's
   internal hashing algorithm is performing poorly on your
   data set.  For example, you stick 10,000 things in a hash,
   but evaluating %HASH in scalar context reveals "1/16",
   which means only one out of sixteen buckets has been
   touched, and presumably contains all 10,000 of your items.
   This isn't supposed to happen.



More information about the Omaha-pm mailing list