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.
In reply to Re: Dispatch table within package question
by ikegami
in thread Dispatch table within package question
by hubb0r
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |