Skip to content

Instantly share code, notes, and snippets.

@hisaichi5518
Last active December 15, 2015 11:09
Show Gist options
  • Select an option

  • Save hisaichi5518/5251060 to your computer and use it in GitHub Desktop.

Select an option

Save hisaichi5518/5251060 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use utf8;
use 5.10.1;
package Parent {
use Mouse;
no Mouse;
my $added;
sub BUILD {
my ($self) = @_;
if (!$added->{$self->meta->name}) {
$self->meta->add_around_method_modifier(as_string => sub {
my $orig = shift;
my $result = $orig->(@_);
return 'extended_'.$result;
});
$added->{$self->meta->name}++;
}
}
sub as_string {
return "parent";
}
say Parent->new->as_string; #=> extended_parent
}
package Child {
use Mouse;
extends 'Parent';
no Mouse;
sub as_string {
return 'child';
}
say Child->new->as_string; #=> extended_child
say Child->new->as_string; #=> extended_child
say Child->new->as_string; #=> extended_child
say Child->new->as_string; #=> extended_child
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment