forrest has asked for the wisdom of the Perl Monks concerning the following question:
I found myself wanting to pass a bunch of methods directly into my embedded object, writing one-line methods like this:sub new { my ($class) = @_; my ($self, $pid); $self = bless +{}, $class; $self->{xmms} = Xmms::Remote->new; # ... more stuff ... }
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 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; }
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 trysub AUTOLOAD { my ($self) = @_; my ($method); ($method = $AUTOLOAD) =~ s/.*://; if ($self->{xmms}->can($method)) { $self->{xmms}-> }
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.$self->{xmms}->$method;
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: AUTOLOAD question
by blokhead (Monsignor) on Nov 29, 2003 at 18:17 UTC | |
|
Re: AUTOLOAD question
by jeffa (Bishop) on Nov 29, 2003 at 19:05 UTC | |
by adrianh (Chancellor) on Dec 01, 2003 at 00:22 UTC | |
|
Re: AUTOLOAD question
by The Mad Hatter (Priest) on Nov 29, 2003 at 18:09 UTC | |
by ysth (Canon) on Nov 30, 2003 at 01:50 UTC | |
|
Re: AUTOLOAD question
by bsb (Priest) on Dec 22, 2003 at 09:14 UTC |