Like yosefm already mentioned, you never actually call anything from AUTOLOAD. Here's a simple (only slightly tested) AUTOLOAD I used for a wrapper class:
sub AUTOLOAD { my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2); my $code_ref = DBI::db->can($method); if (!$code_ref) { require Carp; Carp::croak("Undefined subroutine $method called"); } # This only works with methods, # and only if they are non-static # (just like your code). local $_[0] = $_[0]->{DBH}; goto($code_ref); }
I avoided regexps in case the clobber $1 and similar vars in the caller.
Update: If you wanted to create stubs for possible efficiency:
sub AUTOLOAD { my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::')+2); my $code_ref = DBI::db->can($method); if (!$code_ref) { require Carp; Carp::croak("Undefined subroutine $method called"); } my $stub = sub { # This only works with methods, # and only if they are non-static # (just like your code). local $_[0] = $_[0]->{DBH}; &$code_ref; }; { no strict 'refs'; *$method = $stub; } goto($stub); }
In reply to Re: Using AUTOLOAD to create class methods
by ikegami
in thread Using AUTOLOAD to create class methods
by radiantmatrix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |