Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How to conditionally execute a subroutine defined as hash value

by BrowserUk (Patriarch)
on Apr 02, 2014 at 10:25 UTC ( [id://1080738]=note: print w/replies, xml ) Need Help??


in reply to How to conditionally execute a subroutine defined as hash value

By adding parens, where you are attempting to store the address of your subs:      A => \&print_A(),, you are invoking the subs and then string the address of whatever is returned (the value 1 from print).

Try it this way:

sub print_A { print "\nA\n"; } sub print_B { print "\nB\n"; } $calls = { A=> \&print_A, B=> \&print_B };; $calls->{'A'}();; A

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: How to conditionally execute a subroutine defined as hash value
by Anonymous Monk on Apr 02, 2014 at 10:38 UTC
    Thanks, but if I have to pass some parameters to the call like this?
    use strict; my %config_A = (A=>'a'); my %config_B = (B=>'b'); my $calls = { A => \&print_A(\%config_A), B => \&print_B(\%config_B), }; my $test = 'A'; $calls->{$test}; sub print_A { my $config = shift(); print $config->{A},"\n"; } sub print_B { my $config = shift(); print $config->{B},"\n"; }

      Then you'll need to wrap the calls in anonymous subs:

      my $calls = { A => sub{ print_A( \%config_A ); }, B => sub{ print_B( \%config_B ); }, }; my $test = 'A'; $calls->{$test}();

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Not sure i like the lock-in you get with the anon sub approach. I would make the config hash have both the sub references and the options references -- that way if you ever needed to override the options, it's a bit cleaner

        .... my $calls => { A => { sub_ref => \&print_A, config_ref => { ... } }, B => { sub_ref => \&print_B, config_ref => { ... } } }; my $test = 'A'; my $sub = $calls->{$test}{sub_ref}; my $config = $calls->{$test}{config_ref}; $sub->( $config );

        -derby
        Thanks a lot!

      You need to pass the parameters when you call the function, not when you store the reference.

      See tye's References Quick Reference.

      # Here you take the reference to the subroutine my $calls = { A => \&print_A, B => \&print_B, }; # Here you call the subroutine $calls->{ $test }->(\%config_A);

      When taking the reference, you should never use parentheses.

      Using Data::Dumper on %calls would show you what Perl stored in %calls.

        Thanks but I need to pass %config_A when $test is A, and %config_B when $test is B.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found