Can undef to be a hash key in Perl?

The answer is no.

undef cannot be a hash key in Perl. But there are subtleties which you might think that can. Details below.

What is a hash

One of the data structures that exist in the programming language Perl is the hash. A hash is an unordered set of values. Here is an example hash:

my %h = (
    aa => 1,
    bb => 2,
);

This hash is stored two pairs of values. The key with the name 'aa' stored value 1, and in key with the name 'bb' stores the value 2. To obtain these values, use syntax $h{aa} and $h{bb}.

What is undef

undef is a special value in Perl, which means no value. If you create the variable but not assign it any value, its value will be just undef:

▶ Run
#!/usr/bin/perl

use Data::Dumper;

my $var;

print Dumper $var;

The program will display the text $VAR1 = undef;.

undef is still available if you access a nonexistent array element or a non-existent key in a hash. You can explicitly assign undef variable: $var = undef;.

Incorrect usage 1

Here is a sample program which seems undef may be key hash:

▶ Run
#!/usr/bin/perl

my %h = (
    undef => 'asdf',
);

print $h{undef};

The program displays the text asdf. Ie like we created in the hash key undef, and then I got the value for this key. But it's not. In this example, the value is stored the key 'undef', i.e. row 'undef', not a real undef.

This happens due to two features.

When you create the hash we created it in a couple key-value using code undef => 'asdf',. This code uses operator =>. The peculiarity of this the statement is that in some situations he can take what is left as a string. He concludes like what is left of the => in quotation marks. undef => 'asdf', is actually 'undef' => 'asdf',.

The second feature is how we get the value from hash by key. We use the syntax $h{undef}. And then Perl thinks that what is in the curly braces is a string. What we have written here is the same as $h{'undef'}.

Can be sure that it really is as if you run the script by plugging a special module that instead of executing code shows how Perl understands this code. perl -MO=Deparse script.pl. Here is the output:

my(%h) = ('undef', 'asdf');
print $h{'undef'};
script.pl syntax OK

Incorrect use of 2

Here's another example of code which mistakenly think that undef may be key hash. Oh, there it is we use real undef, not line 'undef':

▶ Run
#!/usr/bin/perl

my $var = undef;

my %h = (
    $var => 'asdf',
);

print $h{$var};

The program displays the text asdf. Ie, do we think that we have created the value in the hash by key undef.

But really, we have placed the value in the key '' (an empty string). If append to the end of this program line use Data::Dumper and warn Dumper \%h;, after starting on the screen that actually looks like a hash:

$VAR1 = {
          '' => 'asdf'
        };

Variable $var contains a value undef, but when used in $var => 'asdf', and $h{$var} undef is replaced by an empty string ''.

If you add in code use strict; and use warnings; (which is almost always worth doing), then in the output it will be seen that Perl warns about this usage:

Use of uninitialized value $var in list assignment at script.pl line 8.
Use of uninitialized value $var in hash element at script.pl line 12.

Summary

undef cannot be a value of a key in Perl hash. The keys in a Perl hash is always a line. (Specify only that an empty string '' can also be the key). And the values in the hash can be absolutely any and there is no problem to be undef.

Other articles

Comments