Monks,
I'm writing a package with numerous subs. At one point a sub will look at the data it is given, and make a choice as to which sub to hand it off to to do the work. This sounded like a good job for a dispatch table.
I'm used to calling other subs in the same package using the:
$self->sub($var);
notation.
I attempted to create the dispatch table like:
my %message_types = (
type1 => \&{$self->sub_1},
type2 => \&{$self->sub_2},
);
and I actually tried a few other notations along the same lines. Each run would result in the value of the hashkey to be undef.
The only way I was able to make this work was with an explicit package name like so:
my %message_types = (
type_1 => \&MY::Fully::Qualified::Package::Name::sub1,
type_2 => \&MY::Fully::Qualified::Package::Name::sub2,
);
The question is, is it possible to use the $self->sub notatation to do this? My assumption was that it was, but you know what they say about assumptions... I'd really rather use the self referencing notation as it looks much cleaner and is much more readable.
Thanks for your help!!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.