#!/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"; } }