I agree with your:
*{ __PACKAGE__ . '::' . $methodname } = $connect->can( $methodname );
... in principle, but in practice it has a subtle bug. (OK, there's the $connect versus $connection typo too, but we'll ignore that as strict mode should make it obvious.)
The first time the autoloaded method is called, the connection object is passed to the method as its invocant. Subsequent calls will get the device as the invocant.
What should be used is something more like:
sub AUTOLOAD { no strict 'refs'; *{__PACKAGE__.'::'.$methodname} = sub { (shift)->getConnection->$methodname(@_) }; return (shift)->getConnection->$methodname(@_); }
Also it's worth noting that whenever you write an AUTOLOAD method, it's worth considering writing a can method, because the default method inherited from UNIVERSAL::can ignores autoloaded methods.
Class::AutoloadCAN explains the issues around AUTOLOAD and can and offers a solution. Personally I've found Class::AutoloadCAN a little buggy myself and ended up re-implementing it on a module-by-module basis (e.g. these __autoload, can and AUTOLOAD methods).
In reply to Re^2: Choosing between AUTOLOAD vs Delegation
by tobyink
in thread Choosing between AUTOLOAD vs Delegation
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |