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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.