package MyDecorator; sub new{ my ($class, $wrapped, %options) = @_; my $self = { wrapped => $wrapped, options => \%options }; bless $self, __PACKAGE__; } sub method1{ my $self = shift; my $wrapped = $self->{'wrapped'} # Do something different than what # $wrapped->method1 would do. # ... } sub method2{ my $self = shift; my $wrapped = $self->{'wrapped'} # method2 is a new method that now provides additional # functionality for the object. } # Implement autoload to defer all the methods we can't handle to the # wrapped class. sub AUTOLOAD{ return if our $AUTOLOAD =~ /::DESTROY$/; my $self = shift; my ($method, @namespace) = reverse(split '::' => $AUTOLOAD); my $wrapped = $self->{'wrapped'}; if ($wrapped->can($method)){ no strict 'refs'; $AUTOLOAD->($wrapped, @_); } else{ die "Cannot run or defer method \"$method\"!"; } }