Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: HASH value as a subroutine call

by Perlbotics (Chancellor)
on Oct 21, 2012 at 10:49 UTC ( #1000199=note: print w/replies, xml ) Need Help??


in reply to HASH value as a subroutine call

You need to populate the hash with code-references (\&sub).

Try:

use strict; # my $code_ref = {'SUM', 'sum'}; my $code_ref = {'SUM', \&sum}; sub sum($$){...} # do you really need ($$) ? That is rarely useful. # ($codes->{'SUM'})->(4,5); # $codes? You mean $code_ref? $code_ref->{'SUM'}->(4,5);

Replies are listed 'Best First'.
Re^2: HASH value as a subroutine call
by AnomalousMonk (Chancellor) on Oct 21, 2012 at 11:46 UTC
    sub sum($$){...} # do you really need ($$) ? That is rarely useful.

    Rarely useful, often misused, and in this case (in which prototypes are not checked at all) totally useless. E.g.:

    >perl -wMstrict -le "sub sum ($$); ;; my $codes = { 'SOFT' => 'sum', 'HARD' => \&sum, }; ;; $codes->{HARD}->('HARD', 1, 2, 3, 4); ;; { no strict 'refs'; $codes->{SOFT}->('SOFT', 9, 8, 7, 6); } ;; sub sum ($$) { print qq{(@_) arguments: }, scalar @_; } " (HARD 1 2 3 4) arguments: 5 (SOFT 9 8 7 6) arguments: 5

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000199]
help
Chatterbox?
[Discipulus]: hello there: Lady_Aleena for inspiration random browse my homenode.. ;=)
[Lady_Aleena]: Discipulus, Hello there. How are you?
[Lady_Aleena]: Good GUH Discipulus! Do you have enough links there?
[Discipulus]: here quite well thanks. still alive. Unfortunately I have the 64Kb limit
Discipulus now is read a book to my daugther time. Just after she and mother stop yelling in the bathroom

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2017-09-24 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (274 votes). Check out past polls.

    Notices?