in reply to Re: How to call a sub reff from a hash
in thread How to call a sub ref from a hash

I think you've goten me on the right track, but this:
#!/usr/bin/perl use strict; use warnings; sub Procedure_Name_1 { my($Parm) = shift; print($Parm); } my %Procedures; $Procedures{'ProcName1'} = \&Procedure_Name_1; my $Procedure = 'ProcName1'; my $Parameter = 'Some Value'; &$Procedures->{$Procedure}($Parameter);
still fails...

Replies are listed 'Best First'.
Re^3: How to call a sub reff from a hash
by Fletch (Bishop) on Dec 15, 2008 at 22:10 UTC

    Because you have no scalar $Procedures declared (you have a hash %Procedures, but that's something completely different).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Yes, but I'm trying to access an element of that hash.

        Your wrong code:

        $Procedures->{$Procedure}($Parameter); ^^^^^^^^^^^ Not a hash. Undeclared and uninitialized scalar.

        The correct code:

        $Procedures{$Procedure}->($Parameter); ^^^^^^^^^^^^^^^^^^^^^^^ Element of a hash
        A reply falls below the community's threshold of quality. You may see it by logging in.

        Then you want to re-read perldata and perlreftut because $foo->{'bar'} has nothing to do with trying to get at $foo{'bar'}.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re^3: How to call a sub reff from a hash
by Joost (Canon) on Dec 15, 2008 at 22:37 UTC
Re^3: How to call a sub reff from a hash
by eric256 (Parson) on Dec 15, 2008 at 22:52 UTC

    Taking your code and then switching the last line like he said makes it work perfectly.

    #!/usr/bin/perl use strict; use warnings; sub Procedure_Name_1 { my($Parm) = shift; print($Parm); } my %Procedures; $Procedures{'ProcName1'} = \&Procedure_Name_1; my $Procedure = 'ProcName1'; my $Parameter = 'Some Value'; $Procedures{$Procedure}->($Parameter);

    I don't understand why you ignored his advice but i would recommend trying it as written in the future.

    BTW you could read the last line as "get the value of $Procedures{$Procedure} and then run it as a coderef ->( passing it $Parameter)"


    ___________
    Eric Hodges