Hey folks! I'm working on a module which embeds Xmms::Remote, starting with something like this:
sub new { my ($class) = @_; my ($self, $pid); $self = bless +{}, $class; $self->{xmms} = Xmms::Remote->new; # ... more stuff ... }
I found myself wanting to pass a bunch of methods directly into my embedded object, writing one-line methods like this:
sub is_paused { $_[0]->{xmms}->is_paused; } sub is_playing { $_[0]->{xmms}->is_playing; } sub pause { $_[0]->{xmms}->pause; } sub play { $_[0]->{xmms}->play; } sub stop { $_[0]->{xmms}->stop; }
I was just about to add another, when I said to myself, "$self, this is stupid. If you used AUTOLOAD correctly, you could automatically have any unrecognized method automatically called on your embedded object". So, I started to try to write such an AUTOLOAD, and I realized I was confused about AUTOLOAD and didn't know how to do it. This is how far I got:
sub AUTOLOAD { my ($self) = @_; my ($method); ($method = $AUTOLOAD) =~ s/.*://; if ($self->{xmms}->can($method)) { $self->{xmms}-> }
That's when I realized, whoa, I have the method in a variable name, that's whack! Since perl often DWIMs, I thought I'd try
$self->{xmms}->$method;
but I don't know if that works or not, because I just got another error which is even more confusing: Global symbol "$AUTOLOAD" requires explicit package name at AlterniRATE/Player.pm line 75.
AlterniRATE/Player.pm had compilation errors.

Whoa ... I thought $AUTOLOAD was supposed to be a special magic variable! I guess I just totally don't know what I'm doing, and for the moment will return to the ugly brute-force method. Can anyone enlighten me as to how I can get AUTOLOAD to do what I want here?

Gee, I guess my oversight was pretty simple (I knew I was just being stupid) but I'm glad I asked, because the responses have all been very enlightening. Thanks, guys!


In reply to AUTOLOAD question by forrest

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.