in reply to method dispatch question

You missed $self->$mref( ... ).

Replies are listed 'Best First'.
Re^2: method dispatch question
by rovf (Priest) on Jul 09, 2008 at 12:15 UTC

    Indeed, I missed it, and it works very well. But now let us make the problem a bit more complicated (I see that I have simplified to much from my original problem): Assume that $mref does not come in as parameter, but will be calculated somehow; for example:

    my $mref=$self->get_mref(...); # Calculate $mref $self->$mref(...); # use it to dispatch
    Can this still be written in one line, without intermediate variable $mref? I learned that my original id
    $self->&{ $self->get_mref(...) }()
    does not work, but extending your suggestion to this case would, I think, fail too:
    $self->($self->get_mref(...))()
    Can this (now more complicated) example be easily solved too?

    -- 
    Ronald Fischer <ynnor@mm.st>
      Even if there is a solution for that (and I don't doubt there is one, perl is full of surprising syntax ;-), you shouldn't go for it, because it's not really readable anymore.

      Use a temporary variable instead.

      $self->($self->get_mref(...))() won't work because $something->(...) treats $something as a sub ref. If there's a way, I suspect it involves ${...}.

        I think $self->${ \ $self->get_mref(...) }(...) would work, but I agree with you about the readability.
      Can this still be written in one line, without intermediate variable $mref?
      $self->get_mref->($self, @args);

      or make get_mref return a code reference that captured $self so you can omit it when calling it.

      sub get_mref { my ($self) = @_; my $coderef = \&some_method; return sub { $self->$coderef(@_) }; } $self->get_mref->(@args);
      There is a way:
      use strict; use CGI; sub meth {'p'} my $cgi = CGI->new; print $cgi->${\meth()}('foo')."\n";
      but why would you want to do that?

      update D'oh! didn't read the question closely enough. This is not what you're looking for.