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!
In reply to AUTOLOAD question by forrest
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |