Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: How to call a sub reff from a hash

by Joost (Canon)
on Dec 15, 2008 at 21:59 UTC ( [id://730507]=note: print w/replies, xml ) Need Help??


in reply to How to call a sub ref from a hash

You want as the last line:
$Procedures{$Procedure}->($Parameter);

Replies are listed 'Best First'.
Re^2: How to call a sub reff from a hash
by NateTut (Deacon) on Dec 15, 2008 at 22:06 UTC
    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...

      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.
          A reply falls below the community's threshold of quality. You may see it by logging in.

      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://730507]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (None)
    As of 2024-04-18 23:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found