fortesque has asked for the wisdom of the Perl Monks concerning the following question:
I have been trying various ways to call multiple subroutines(windows XP, OS) (I'm writing a Tk program that has serveral hundred Tk callbacks) and so far have been unsuccessful. I'm a beginner and my Perl knowledge is limited, and more mechanical than intuitive.
The following experimental code calls only the first subroutine not the second.
It seems to
recognize both subroutines but executes only the first one.
Can anyone help me figure out how to get the code to call the second subroutine as well?
Both subroutines work when run as standard named routines and not part of a hash.
#when code is run, "the answer is 9" is printed along with "moo is the answer" and "foo is the answer".#!/usr/local/bin/perl use warnings; my $key; my $dispatch; my %dispatch = ( "foo" => sub { my $x = 3; my $ans = ($x**2); print "answer is: $ans \n"; #prints "answer is: 9" }, "moo" => sub { my @data = (3,7,15,28); my $radius; my $radius_ref = @data; foreach $radius(@$radius_ref) { print "my radius is: $radius \n"; my $area = 3.14159 * ( $radius ** 2); print "and circle area is $area \n"; } } ); #Perl PCB 5.2 reference my $input; foreach $input("foo", "moo"){ if ( exists ${dispatch}{ $input } ) { ${dispatch} {$input}( ); print "$input is the answer.\n"; } else { die "Cannot find the subroutine $input\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: call subroutines via hash
by eric256 (Parson) on Jan 04, 2006 at 21:00 UTC | |
by fortesque (Acolyte) on Jan 04, 2006 at 21:36 UTC | |
|
Re: call subroutines via hash
by smokemachine (Hermit) on Jan 04, 2006 at 21:00 UTC | |
by fortesque (Acolyte) on Jan 04, 2006 at 21:54 UTC | |
by swkronenfeld (Hermit) on Jan 04, 2006 at 22:40 UTC | |
by Errto (Vicar) on Jan 04, 2006 at 22:27 UTC | |
|
Re: call subroutines via hash
by xdg (Monsignor) on Jan 04, 2006 at 20:55 UTC | |
by Roy Johnson (Monsignor) on Jan 04, 2006 at 21:05 UTC | |
by eric256 (Parson) on Jan 04, 2006 at 21:02 UTC |