Saved has asked for the wisdom of the Perl Monks concerning the following question:

I would like to do the following, but pass a parameter, and get the return. I have tried:
$Content= $actions{$RFlds[3]}->($EnvName);
But It's not working. Any help would be much appreciated. Thanx. Implementing Dispatch Tables The trivial case A basic dispatch table might look like this: my %disp_table = ( something => \&do_something ); download You put a string in, you get a code ref out. Then, presumably, you execute the code ref: $disp_table{'something'}->(); download What could be simpler?

Replies are listed 'Best First'.
Re: Dispatch table with parameter
by rjt (Curate) on Jun 03, 2013 at 14:41 UTC

    You would do well to post the actual code you are running; without the do_something sub, we can't give you specific help. I can, however, confirm that dispatch table subs can take parameters:

    my %dispatch = ( something => \&my_sub ); $dispatch{something}->("Hello, world."); sub my_sub { my $param = shift; print "my_sub says `$param'"; } __END__ Output: my_sub says `Hello, world.'
Re: Dispatch table with parameter
by tobyink (Canon) on Jun 03, 2013 at 14:44 UTC
    #!/usr/bin/env perl use 5.010; use strict; use warnings; my %table = ( uppercase => sub { uc $_[0] }, lowercase => sub { lc $_[0] }, mixedcase => sub { ucfirst lc $_[0] }, ); say $table{"uppercase"}->("Foo"); say $table{"lowercase"}->("Bar"); say $table{"mixedcase"}->("Baz");
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Thanx much.