in reply to Re: null keys
in thread null keys

Are we talking empty string or undef here? They act differently, depending.

Normally they do, but we're talking about hash keys here. A hash key can only be a string, so any number, reference or undefined value is stringified. undef happens to stringify to the emtpy string, so $foo{+undef} and $foo{''} are the same thing.

2;0 juerd@ouranos:~$ perl -le'$foo{+undef} = "Hello, world!"; print $f +oo{""}' Hello, world!

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
Re: Re: Re: null keys
by marvell (Pilgrim) on Jun 12, 2002 at 16:27 UTC
    Which is shown nicely by:
    #!/usr/bin/perl use Data::Dumper; $hash{+undef} = "pleasure"; $hash{''} = "pain"; print Dumper(\%hash);

    Outputs:

    $VAR1 = { '' => 'pain', };

    --
    Steve Marvell

Re: Re: Re: null keys
by marvell (Pilgrim) on Jun 12, 2002 at 16:26 UTC
    Update: I tried to cancel the submission and ended up in another double submission fiasco. I realied my error, hence the latter post.

    --
    Steve Marvell

      sub bar () { 'quux' } $foo{undef} = 'bareword undef is treated as string'; $foo{+undef} = 'function undef is treated as function because + is not valid in a bareword string'; $foo{bar} = 'key bar is used'; $foo{+bar} = 'key quux is used'; $foo{bar()} = 'key quux is used again (the old value is overwritten)';

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

      If the stuff inside the curlies looks like a word it's automaticaly quoted.

      Thus $hash{undef} and $hash{'undef'} are equivalent.Try :

      $ perl -MO=Deparse -e "print $hash{undef}"

      If you want to use an undef as a hash key you have to use either $hash{+undef} or something like my $NULL = undef; $hash{$NULL}

      Update: Erm ... hash keys are strings. Always. So $hash{+undef} us equivalent to $hash{''}. Oh well.

        Jenda