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).

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re^2: Choosing between AUTOLOAD vs Delegation by tobyink
in thread Choosing between AUTOLOAD vs Delegation by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.