Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Call to subs in a Dispatch Table

by soonix (Canon)
on May 08, 2015 at 11:50 UTC ( [id://1126079]=note: print w/replies, xml ) Need Help??


in reply to Call to subs in a Dispatch Table

With "at the same time" you don't want to say "in parallel", do you?

If you want to call the code from the "data" key, you can do so if you separate declaration from assignment, like this
my %general; %general = ( ...
then you can call it from "inside", like this:
results => sub { names( first => $q->param('first'), zip => $q->param('zip'), ); $general{data}->(); },
BEWARE this creates a circular reference, which probably won't be a problem here, but you should be aware of the fact.

Update: OOPS! of course I didn't mean to call $general{results} - thanks AnomalousMonk - corrected

Further Update: obviously, the interpreter is less strict than the manual: according to perlref I'm allowed to say &{$general{data}}() ("method 2") or $general{data}->() ("method 3", but when I tried it out before posting (with a simpler example), the syntax $general{data}() also worked. Perhaps this is a version specific thing (strawberry 5.14.2.1)

Replies are listed 'Best First'.
Re^2: Call to subs in a Dispatch Table
by AnomalousMonk (Archbishop) on May 08, 2015 at 13:54 UTC
    ... this creates a circular reference, which probably won't be a problem ...

    It also creates a circular, i.e., infinite, execution loop, which probably will be a problem.

    c:\@Work\Perl\monks>perl -wMstrict -le "sub names { print qq{@_}; } ;; my %dispatch; %dispatch = ( circular => sub { names('fu', 'bar'); sleep 1; $dispatch{circular}->(); }, straight => sub { names('a', 'ok'); }, ); ;; $dispatch{straight}->(); $dispatch{circular}->(); " a ok fu bar fu bar fu bar fu bar Terminating on signal SIGINT(2)


    Give a man a fish:  <%-(-(-(-<

Re^2: Call to subs in a Dispatch Table
by AnomalousMonk (Archbishop) on May 10, 2015 at 07:23 UTC
    ... the interpreter is less strict than the manual... Perhaps this is ... version specific ...

    It is not version specific, as demonstrated by this example under ActiveState 5.8.9:

    c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; my %hash = ( func => sub { print qq{hi $_[0]}; }, elem => [ 'fee', 'fie', 'foe', ], ); ;; $hash{func}('there'); $hash{func}->('sailor'); ;; print $hash{elem}[2]; print $hash{elem}->[1]; " perl version 5.008009 hi there hi sailor foe fie
    AFAIK, this is universal.

    As I mentioned in my /msg to you, I also thought the syntax  $general{data}() would not work. I'm sure this is discussed somewhere, but I haven't had the time yet to search it out. Basically, use of  -> (see The Arrow Operator in perlop) is only required at "top" level in a nested hash, array or code reference chain that's accessed initially by reference. That's because everything below the topmost level in a multi-dimensional structure must always be a reference of some kind (if it's not a simple string or number), and Perl implicitly understands this.


    Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found