Search code examples
stringperlconcatenationperl-data-structures

In Perl, why does concatenating a hash with a string give a fraction-looking result?


I have the following hash:

my %villains = {
    "Boba" => "Fett",
    "Darth" => "Vader",
    "Moff" => "Tarkin",
}

I then print it like so:

print "".%villains;

I get the following output:

1/8

What semantics in Perl make this happen?

Thank you!


Solution

  • You are evaluating the hash in scalar context. When you do this it actually returns a fraction of the number of buckets touched over the total number of buckets, unless the hash has not been used in which case it evaluates to false. See this perldoc for more info (near the end of the section).