http://qs321.pair.com?node_id=701968


in reply to Re^2: messing with @ISA - unblessing
in thread messing with @ISA - unblessing

I don't understand how that's a problem. The AUTOLOAD goes in the Decorator class, not the original object's class. The Decorator is new, so I wouldn't expect it to subclass anything else...?

Of course, you can implement a Decorator class without AUTOLOAD, it's just more tedious as you have to write a little method each time, and maintain the Decorator as the original object's class changes. Based off your example, maybe something like this:

package Curse::Older; sub new{ my ($class, $player, $older_by) = @_; my $self = { player => $player, older_by => $older_by, cursed => 1 }; bless $self, $class; } # name is just simply delegated sub name{ my $self = shift; $self->{'player'}->name(@_); } # age is however intercepted and modified sub age{ my $self = shift; my $player = $self->{'player'}; my $age = $player->age(); if ($self->{'cursed'}){ $age + $self->{'older_by'}; } else{ $age; } } # Meanwhile... package main; my $player = new Player('Jim', 30); # curse Jim to make him 10 years older my $cursed_player = Curse::Older->new($player, +10);

Anyway, it looks like you're going down the re-blessing route. The Java programmer in me thinks that's nuts, but the Perl hacker in me admires what the language lets you do!