in reply to Re: multiple keys - one value
in thread using a hash of functions

I'm trying to improve on sandfly's code by eliminating the need for temp variables, but getting into trouble. Why does the following code produce
"Use of uninitialized value in subroutine entry at -e line 1.
Undefined subroutine &main:: called at -e line 1."
my %h=( HELP => sub {print qq(help\n)}, Q=>$h{HELP}, SAVE=>sub{print qq(save\n)} ); print $h{Q}();
I have also tried it by separating the declaration and population of %h, to no avail.

Replies are listed 'Best First'.
Re: Re: Re: multiple keys - one value
by QM (Parson) on Dec 04, 2003 at 07:10 UTC
    I suspect it's because $h{HELP} doesn't exist when you create the tuple Q=>$h{HELP}, as %h hasn't been assigned to at that time.

    Perhaps this will work for you:

    use Data::Dumper; my %h=( HELP => sub {print qq(help\n)}, SAVE=>sub{print qq(save\n)} ); $h{Q} = $h{HELP}; print Dumper(%h); print $h{Q}();
    Result:
    $VAR1 = 'Q'; $VAR2 = sub { "DUMMY" }; $VAR3 = 'HELP'; $VAR4 = $VAR2; $VAR5 = 'SAVE'; $VAR6 = sub { "DUMMY" }; help 1
    Note that HELP and Q both use the same sub.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of