From: gfx Date: Sun, 4 Oct 2009 06:45:58 +0000 (+0900) Subject: Refactor the override method modifier code to reduce duplications X-Git-Tag: 0.37_02~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=85bd3f44fbee036b39fedb1e2ca2b566c1a6d08f;p=gitmo%2FMouse.git Refactor the override method modifier code to reduce duplications --- diff --git a/lib/Mouse.pm b/lib/Mouse.pm index 4bf8ab3..68808b8 100644 --- a/lib/Mouse.pm +++ b/lib/Mouse.pm @@ -86,27 +86,14 @@ our @SUPER_ARGS; sub super { # This check avoids a recursion loop - see # t/100_bugs/020_super_recursion.t - return if defined $SUPER_PACKAGE && $SUPER_PACKAGE ne caller(); - return unless $SUPER_BODY; $SUPER_BODY->(@SUPER_ARGS); + return if defined $SUPER_PACKAGE && $SUPER_PACKAGE ne caller(); + return if !defined $SUPER_BODY; + $SUPER_BODY->(@SUPER_ARGS); } sub override { - my $meta = Mouse::Meta::Class->initialize(caller); - my $pkg = $meta->name; - - my $name = shift; - my $code = shift; - - my $body = $pkg->can($name) - or confess "You cannot override '$name' because it has no super method"; - - $meta->add_method($name => sub { - local $SUPER_PACKAGE = $pkg; - local @SUPER_ARGS = @_; - local $SUPER_BODY = $body; - - $code->(@_); - }); + # my($name, $method) = @_; + Mouse::Meta::Class->initialize(scalar caller)->add_override_method_modifier(@_); } sub inner { not_supported } diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index 765def8..bfcbaa0 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -397,12 +397,23 @@ sub add_after_method_modifier { sub add_override_method_modifier { my ($self, $name, $code) = @_; + if($self->has_method($name)){ + $self->throw_error("Cannot add an override method if a local method is already present"); + } + my $package = $self->name; - my $body = $package->can($name) + my $super_body = $package->can($name) or $self->throw_error("You cannot override '$name' because it has no super method"); - $self->add_method($name => sub { $code->($package, $body, @_) }); + $self->add_method($name => sub { + local $Mouse::SUPER_PACKAGE = $package; + local $Mouse::SUPER_BODY = $super_body; + local @Mouse::SUPER_ARGS = @_; + + $code->(@_); + }); + return; } sub does_role { diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index bc32665..956ab99 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -57,29 +57,13 @@ sub around { sub super { - return unless $Mouse::SUPER_BODY; + return if !defined $Mouse::SUPER_BODY; $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS); } sub override { - my $classname = caller; - my $meta = Mouse::Meta::Role->initialize($classname); - - my $name = shift; - my $code = shift; - my $fullname = "${classname}::${name}"; - - defined &$fullname - && $meta->throw_error("Cannot add an override of method '$fullname' " - . "because there is a local version of '$fullname'"); - - $meta->add_override_method_modifier($name => sub { - local $Mouse::SUPER_PACKAGE = shift; - local $Mouse::SUPER_BODY = shift; - local @Mouse::SUPER_ARGS = @_; - - $code->(@_); - }); + # my($name, $code) = @_; + Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_); } # We keep the same errors messages as Moose::Role emits, here.