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
    This explains a lot, thanks ikegami. I'm still getting my feet wet with using a more oop approach to perl. Your answer and Aristotle's are exactly what I was looking for!
Re^2: Dispatch table within package question
by loris (Hermit) on Oct 07, 2005 at 09:25 UTC

    Presumably, the method which contains the definition of the dispatcher will be called frequently. Is there any performance related disadvantage to this for, say, the case that the dispatcher hash is very large?

    And if I did want to define the dispatcher as a member in the constructor along the lines of:

    my $self->{message_types} = ( type1 => sub { $self->sub_1(@_); }, type2 => sub { $self->sub_2(@_); }, );

    what would the call the dispatcher look like?

    I tried:

    $self->{message_types}->{$type}->(@args);

    but as what I am doing is beyond my understanding of Perl hairiness, I wasn't suprised to find that it doesn't work. What would the correct code look like?

    Thanks,

    loris


    "It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ."

      Perl implements hashes such that lookups are extremely efficient. The size of the hash doesn't matter.

      Your syntax,

      $self->{message_types}->{$type}->(@args);

      should be fine. What's the error? What's your code? Remember, that won't use the current value of $self, but the one $self had when the anonymous sub was created (assuming $self was a lexical (my) variable).

        Sorry, you're right, it does work as you say. I had tried it out in an existing module with a whole lot of other gubbins with seemed mess things up (or at least prevent me from seeing clearly what I was doing).

        Thanks again for the help.

        loris


        "It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ."