in reply to Dispatch table within package question
If you want to use $self as defined when %message_types is defined, use:
my %message_types = ( type1 => sub { $self->sub_1(@_); }, type2 => sub { $self->sub_2(@_); }, ); $message_types{$type}->(@args);
If you want to use $self as defined when %message_types is used, use:
my %message_types = ( type1 => 'sub_1', type2 => 'sub_2', ); $method = $message_types{$type}; $self->$method(@args);
You can also use a hybrid:
my %message_types = ( type1 => sub { $object->sub_1(@_); }, type2 => sub { $object->sub_2(@_); }, type3 => 'sub_3', type4 => 'sub_4', type5 => \&bla, type6 => \&Foo::bar, ); $func = $message_types{$type}; if (ref($func)) { # It's a function. $func->(@args); } else { # It's a method. $self->$func(@args); }
Update: Added missing "$" in front of some "func"s.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Dispatch table within package question
by hubb0r (Pilgrim) on Jan 26, 2005 at 18:00 UTC | |
|
Re^2: Dispatch table within package question
by loris (Hermit) on Oct 07, 2005 at 09:25 UTC | |
by ikegami (Patriarch) on Oct 07, 2005 at 14:55 UTC | |
by loris (Hermit) on Oct 14, 2005 at 06:59 UTC |