in reply to Dispatch table within package question

you can also use can to get a code reference to an objects method. i.e.
sub sub { my ($self,$type,@args) = @_; my %message_types = ( type1 => $self->can("type1"), type2 => $self->can("type2") ); return $message_types{$type}->($self,@args); }
Make sure that when you call the methods like this (i.e. $message_types{$type}->()) that you take into account that $self won't automatically be part of @_ as if you had called it like $self->method(). You have to explicitly pass it in your dispatcher method

Replies are listed 'Best First'.
Re^2: Dispatch table within package question
by chromatic (Archbishop) on Jan 26, 2005 at 21:53 UTC

    I prefer:

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

    To me, it looks more like a method call.

    Update: Yep, invoking a coderef from a hash is a little too complex for the parser to handle.

      that's fine, unfortunately, I can't get that to compile... Maybe you were thinking something like:
      my $method = $message_types{$type}; $self->$method(@args);
      Which I agree looks more like a method call and avoids having to pass $self manually -- Brian