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"; } }
In reply to call subroutines via hash by fortesque
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |