hakkr has asked for the wisdom of the Perl Monks concerning the following question:

Being fond of guessing syntax and inventing dangerous constructs today I have been using null values as keys in my hashes. Whats the goosy with that then, I assume it is allowed so you can catch bad key look ups $hash->{''}="a value"; I have found this handy when calling function refs that are stored in hashes &$hash{$params->{action}}->(). I tried using autoload but my sub dereferance messed up with null values and did't call nuthin until i introduced this null key. Also can I push null values onto arrays I tried and I don't think it worked.

Replies are listed 'Best First'.
Re: null keys
by Abigail-II (Bishop) on Jun 12, 2002 at 16:12 UTC
    I am not sure if I quite understand your question. Did you actually ask a question? Anyway, some remarks.

    First, there is no such thing as a "null" value in Perl. Perhaps you mean an undefined value, or perhaps you mean an empty string. If you try to use an undefined value as a hash key, Perl will act as if you tried to use an empty string. Because that's what Perl does with undefined values being used as strings.

    I am not sure whether you realize what the syntax &$hash{$params->{action}}->() means. First of all, it's a syntax error, the right syntax would be &{$hash{$params->{action}}}->(). But that assumes $hash{$params->{action}} stores a code reference, whose return value is another code reference.

    As for pushing things to arrays, any scalar or list value can be pushed to an array. Including undefined values and empty strings.

    Abigail

      The syntax works fine for me, $hash is just the hash of code referances $params->{action} is just the key for which one to call. Here is the surrounding code.

      my $q =new CGI; my $params=$q->Vars(); my %hash=('sub1'=>\&sub1,'sub2'=>\&sub2, ''= >\&notfound); main(); sub main { #call the sub as defined by cgi param 'action' &$hash{$params->{'action'}}->() ; }

      Thanks all for the tip on pushing undef, don't know if I asked much of a question was just checking this is 'normal' behaviour Update My mistake somehow added quotes when retyping

        The syntax works fine for me

        For which version of Perl and which platform? I just tried compiling the program on every version of Perl since 5.000, and it just doesn't compile on any version.

        $hash is just the hash of code referances

        No, it's not. The values in the hash are strings, not code references. Had you dropped the quotes, then it would be code references.

        Abigail

Re: null keys
by particle (Vicar) on Jun 12, 2002 at 16:04 UTC
    i don't quite understand your question. what 'messed up'? can you show a small example?

    as for pushing null values onto arrays, you can push *undefined* values onto arrays push @a, undef;

    ~Particle *accelerates*

Re: null keys
by Joost (Canon) on Jun 12, 2002 at 16:12 UTC
    You are not using NULL keys (whatever those are) in your example but empty strings. Also, it's very easy to put empty strings in arrays:

    my @array = ('a','','b',''); print "'$_'\n" for @array;
    Other then that - I don't understand what your question is. Please show us some code that doesn't work, maybe we can help you with that. :-)
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: null keys
by marvell (Pilgrim) on Jun 12, 2002 at 16:08 UTC
    Are we talking empty string or undef here? They act differently, depending.

    --
    Steve Marvell

      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.
      

        Which is shown nicely by:
        #!/usr/bin/perl use Data::Dumper; $hash{+undef} = "pleasure"; $hash{''} = "pain"; print Dumper(\%hash);

        Outputs:

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

        --
        Steve Marvell

        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