Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: messing with @ISA - unblessing

by Boldra (Deacon)
on Aug 03, 2008 at 09:55 UTC ( #701878=note: print w/replies, xml ) Need Help??


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

Many thanks for your suggestion, but AUTOLOAD only lets me define new methods, not redefine old ones, which is one of my aims.

Replies are listed 'Best First'.
Re^3: messing with @ISA - unblessing
by crashtest (Curate) on Aug 04, 2008 at 04:57 UTC

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-08-15 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (78 votes). Check out past polls.

    Notices?