From: Matt S Trout Date: Sat, 13 Nov 2010 22:24:17 +0000 (+0000) Subject: don't try and apply modifiers during role composition X-Git-Tag: 0.009001~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=dccea57d17d218806e5770d0044c85e30c38e8da;p=gitmo%2FRole-Tiny.git don't try and apply modifiers during role composition --- diff --git a/lib/Moo.pm b/lib/Moo.pm index fdaec57..2f27387 100644 --- a/lib/Moo.pm +++ b/lib/Moo.pm @@ -33,6 +33,7 @@ sub import { }; foreach my $type (qw(before after around)) { *{_getglob "${target}::${type}"} = sub { + require Class::Method::Modifiers; _install_modifier($target, $type, @_); }; } diff --git a/lib/Moo/Role.pm b/lib/Moo/Role.pm index 48a3bb0..7a7cae1 100644 --- a/lib/Moo/Role.pm +++ b/lib/Moo/Role.pm @@ -51,11 +51,9 @@ sub create_class_with_roles { return $new_name; } -sub _install_modifiers { - my ($me, $to, $modifiers) = @_; - foreach my $modifier (@{$modifiers||[]}) { - _install_modifier($to, @{$modifier}); - } +sub _install_single_modifier { + my ($me, @args) = @_; + _install_modifier(@args); } sub _handle_constructor { diff --git a/lib/Moo/_Utils.pm b/lib/Moo/_Utils.pm index ad0b5b1..6bf8f22 100644 --- a/lib/Moo/_Utils.pm +++ b/lib/Moo/_Utils.pm @@ -9,12 +9,12 @@ sub _getglob { no strict 'refs'; \*{$_[0]} } sub _install_modifier { my ($into, $type, $name, $code) = @_; - my $ref = ref(my $to_modify = $into->can($name)); - require Sub::Defer; - Sub::Defer::undefer_sub($to_modify); + if (my $to_modify = $into->can($name)) { # CMM will throw for us if not + require Sub::Defer; + Sub::Defer::undefer_sub($to_modify); + } - require Class::Method::Modifiers; Class::Method::Modifiers::install_modifier(@_); } diff --git a/lib/Role/Tiny.pm b/lib/Role/Tiny.pm index 1a4cf9c..bcaa374 100644 --- a/lib/Role/Tiny.pm +++ b/lib/Role/Tiny.pm @@ -200,11 +200,20 @@ sub _install_methods { sub _install_modifiers { my ($me, $to, $modifiers) = @_; - foreach my $modifier (@{$modifiers||[]}) { - Class::Method::Modifiers::install_modifier($to, @{$modifier}); + if (my $info = $INFO{$to}) { + push @{$info->{modifiers}}, @{$modifiers||[]}; + } else { + foreach my $modifier (@{$modifiers||[]}) { + $me->_install_single_modifier($to, @$modifier); + } } } +sub _install_single_modifier { + my ($me, @args) = @_; + Class::Method::Modifiers::install_modifier(@args); +} + sub does_role { my ($package, $role) = @_; return exists $APPLIED_TO{$package}{$role}; diff --git a/t/moo.t b/t/moo.t index 4656944..2ba2a00 100644 --- a/t/moo.t +++ b/t/moo.t @@ -49,6 +49,8 @@ is_deeply( [ @MyClass3::ISA ], [ 'MyClass2' ], 'extends sets superclass' ); +{ package WhatTheFlyingFornication; sub wtff {} } + { package MyClass4;