Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

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


in reply to Re^2: How to conditionally execute a subroutine defined as hash value
in thread How to conditionally execute a subroutine defined as hash value

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.
  • Comment on Re^3: How to conditionally execute a subroutine defined as hash value
  • Download Code

Replies are listed 'Best First'.
Re^4: How to conditionally execute a subroutine defined as hash value
by derby (Abbot) on Apr 02, 2014 at 11:15 UTC

    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
      Not sure i like the lock-in you get with the anon sub approach.

      Personally, I see your method causing more lock-in than mine. You're committing them to a single parameter.

      Of course, you can then make it an anonymous array, and pass that.

      But now you are creating yet another level of indirection and nesting, and parallel data structures, all of which then needs to be mentally unwound to work out what gets passed to what.

      The delegate -- anonymous sub that encapsulates code references along with their arguments (by closure) -- is a very well tried and tested mechanism used by FP languages almost exclusively. It's clear, concise and can usually be in-lined.


      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.
Re^4: How to conditionally execute a subroutine defined as hash value
by Anonymous Monk on Apr 02, 2014 at 10:50 UTC
    Thanks a lot!

Log In?
Username:
Password:

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

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

    No recent polls found