Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: How to call a sub reff from a hash

by NateTut (Deacon)
on Dec 15, 2008 at 22:06 UTC ( [id://730511]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://730511]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-23 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found