30 October 2011

Use of uninitialized value in hash element: the reason for this warning in Perl

Once again it proved to be hard to find more information about a Perl warning, this time about the "Use of uninitialized value in hash element" one, despite it being what it says on the tin. One could (correctly) guess that it refers to a hash key, but what might be confusing is that Perl has no problems with

$a{undef} = 1;

Instead, it seems that this error is connected to one of the nice features of Perl: autovivification, as the following code does make Perl issue a warning:

$a{$b{1}} = 1;

So if you see this, look out for undefined references used as hash keys.