0やundef要素をHashに入れた時の参照方法テスト

Perlは0をundefとしてみなす習慣があるが、Hash Referenceに0,undef,undefという文字列を入れた時に区別するのだろうか?というテスト。結果こうなりました。

my @arr = (undef,0,1);

my $hash = +{
    $arr[0] => 'a',
    $arr[1] => 'b',
    $arr[2] => 'c',
    undef => 'd',
};

my $undef = undef;

print $hash->{$undef};  # a
print $hash->{0};   # b
print $hash->{1};   # c
print $hash->{undef};   # d

全部区別されますね!