Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^3: messing with @ISA - unblessing

by crashtest (Curate)
on Aug 04, 2008 at 04:57 UTC ( #701968=note: print w/replies, xml ) Need Help??

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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://701968]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2020-11-27 20:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found