AUTOLOAD

Matt Sergeant msergeant at messagelabs.com
Wed Jan 17 23:47:05 GMT 2007


On 17-Jan-07, at 8:36 AM, chromatic wrote:

> Note also that:
>
> 1) can() returns a subroutine reference.  Don't break that.
>
> 2) can() is a method.  Methods aren't functions.  If you call a  
> method as a
> function, you've introduced unreliability into everyone's code  
> again, and of
> course things will break.

A good implementation of AUTOLOAD where you've implemented ->can()  
right might be this (subject to the implementations returned from - 
 >can() not changing based on object state):

sub AUTOLOAD {
   my $method = $AUTOLOAD;
   $method =~ s/.*:://;
   my $implementation = $_[0]->can($method)
     || croak "Can't locate method \"$method\" via thing $_[0]";
   no strict 'refs';
   *{$AUTOLOAD} = $implementation;
   goto &$AUTOLOAD;
}

Don't forget to do something about DESTROY though. It always bothered  
me that there wasn't an empty UNIVERSAL::DESTROY - why should an  
unimplemented DESTROY drop into AUTOLOAD?


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________


More information about the london.pm mailing list