It looks to me as if undef used as a hash key gets turned into an empty string.
use Test::More tests => 6; my %h = ( undef() => 1 ); foreach my $key ( keys %h ) { ok( defined $key, 'key is defined' ); is( $key, '', 'key is empty string' ); } is_deeply( [ '' ], [ keys %h ], 'undef key' ); is_deeply( [ 1 ], [ values %h ], 'values works' ); is( $h{undef()}, 1, 'undef key accesses value' ); is( $h{''}, 1, 'empty string key accesses value' );
Even when I try to be rude and slip it in there, it won't go.
package UndefKey; sub TIEHASH { bless [], __PACKAGE__ } sub FIRSTKEY { undef } sub NEXTKEY { undef } sub FETCH { 1 } package main; #my %h = ( undef() => 1 ); tie my %h, 'UndefKey';
(Running the same tests as before, only the last two succeed, and the ones in the foreach do not run because keys returns an empty list.)
In reply to Re: undef as a key in a hash.
by kyle
in thread undef as a key in a hash.
by fenLisesi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |