In my experience, monkey patching usually is not worth its trouble. The problem you describe above I would solve by subclassing + duck typing: in Example::ErrorList, provide a method that returns list item class name, and instantiate objects of that name. This way you can subclass both Example::ErrorList and Example::Error in a "clean" way.
Like this:
package Example::ErrorList; sub list_item_class { 'Example::Error' } sub new { my ($class) = @_; my $item_class = $class->list_item_class; my $self = bless {}, $class; $self->{items} = [ map { $item_class->new($_) } @seed_list_or_some +thing ]; return $self; } package Example::Error; sub as_string { die "as_string should be implemented by subclass"; } sub asplode { my ($self) = @_; die "BANG! ".$self->as_string; } package My::AwesomeErrorList; use base 'Example::ErrorList'; sub list_item_class { 'My::AwesomeError' } package My::AwesomeError; use base 'Example::Error'; sub as_string { # Actually implement it }
You also have a typo: my $self = @_; won't work the way you expect.
Regards,
Alex.
In reply to Re: Safer monkey-patching
by dwalin
in thread Safer monkey-patching
by tobyink
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |