Perl Gem, Ambiguous Curlies

From Bilbo
Jump to: navigation, search

2005-08-03

This is quite a perl gem. These statements should do exactly the same thing. Thanks to the multiple meanings of {}, their interpretations are wildly different.

print Dumper(eval '{1 => 2}');
print Dumper(eval '{"1" => 2}');

Generates:

$VAR1 = 1;
$VAR2 = 2;
$VAR1 = {
          1 => 2
        };

2006-09-26 Update: A year later, this doesn't happen anymore. I should've recorded which version of Perl it was. I know I wasn't crazy; I spent a very long time debugging this in a program, and finally extracted these lines to demonstrate to myself that I was not, in fact, crazy. In any case, the point is still valid: It's a terrible idea to overload symbols with too many meanings. Perl uses {} to represent both a statement block and a ref to an anonymous hash, and the grammar does not fully disambiguate them.