in reply to goto and AUTOLOADed methods

Just combine your first two examples and use the goto:
sub AUTOLOAD { my $self = $_[0]; my $method = $self->something_clever_returning_method_ref(); # or my $method = something_clever_returning_method_ref($self); # or whatever goto &$method; }

Update: Change "shift" to $_[0].

Also add recommendation here: something_clever_returning_method_ref() should instantiate the method as a sub so that next time it gets called directly.

Replies are listed 'Best First'.
Re: Re: goto and AUTOLOADed methods
by fergal (Chaplain) on Aug 01, 2003 at 11:20 UTC
    That breaks because you've shifted $self off the stack so it's no longer the first argument when you goto $method.

    The question is really about, how to avoid leaving an entry for the AUTOLOAD subroutine on the call stack. There's a well established way of doing it when you're AUTOLOADing functions but I've never seen an example of AUTOLOADing methods used the magic stack fixing goto. Is this because there's some subtle problem I've missed or is it just because nobody has bothered?

      Ok, I fixed that. Just grab the first argument as $_[0], do whatever you want with it to figure out what method you need to call, require other modules, build a new sub, etc., then just use goto to call the method you want with the arguments (including the object) intact. The goto will mimic a method call just like you want it to.
Re: Re: goto and AUTOLOADed methods
by nite_man (Deacon) on Aug 01, 2003 at 11:19 UTC

    First argument should be object reference when you call object method:

    sub AUTOLOAD { my $self = shift; my $method = $self->something_clever_returning_method_ref(); # or my $method = something_clever_returning_method_ref($self); # or whatever goto &$method($self); }

    _ _ _ _ _ _
      M i c h a e l

      But you haven't passed any of the other arguments now.

      The whole point of the magic goto &$sub is that you don't pass any arguments at all, the subroutine gets the current @_. It makes it look like the AUTOLOAD never happened, even if the method uses the caller() function to look at who called it, it won't see the AUTOLOAD routine, it will see the code that called the AUTOLOADer.

Re: Re: goto and AUTOLOADed methods
by fergal (Chaplain) on Aug 01, 2003 at 11:54 UTC
    I'm not doing what you think I'm doing with AUTOLOAD, so instantiating sub routines doesn't come into it. Have a look below to see some more detail on why I'm AUTOLOADing.