ehdonhon has asked for the wisdom of the Perl Monks concerning the following question:
sub first { print "This is first\n"; } sub second { print "This is second\n"; } sub dispatch { no strict 'refs'; my $command = shift; print "In dispatch, command = $command\n"; my %dispatch = ( 'a'=>'first', 'b'=>'second' ); &{$dispatch{$command}}(); } dispatch('a'); dispatch('b');
In dispatch, command = a This is first In dispatch, command = b This is second
package FOO; sub first { print "This is first\n"; } sub second { print "This is second\n"; } sub dispatch { my $self = shift; no strict 'refs'; my $command = shift; print "In dispatch, command = $command\n"; my %dispatch = ( 'a'=>'first', 'b'=>'second' ); &{$self->{$dispatch{$command}}}(); } sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } package ::; my $foo = new FOO(); $foo->dispatch('a'); $foo->dispatch('b');
Undefined subroutine &main:: called at ./dispatch2.pl line 15.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 1: Dispatching Method Objects
by tilly (Archbishop) on Oct 03, 2001 at 21:23 UTC | |
|
Re: Dispatching Method Objects
by gbarr (Monk) on Oct 03, 2001 at 21:25 UTC | |
|
Re: Dispatching Method Objects
by Masem (Monsignor) on Oct 03, 2001 at 21:17 UTC | |
|
Re: Dispatching Method Objects
by Fletch (Bishop) on Oct 03, 2001 at 21:24 UTC | |
|
Re: Dispatching Method Objects
by suaveant (Parson) on Oct 03, 2001 at 21:27 UTC | |
|
Re: Dispatching Method Objects
by runrig (Abbot) on Oct 03, 2001 at 21:22 UTC | |
by tilly (Archbishop) on Oct 03, 2001 at 21:26 UTC |