From: Matt S Trout Date: Mon, 14 Nov 2011 23:44:58 +0000 (+0000) Subject: also update Role::Tiny to handle VSTRING and credit doy for pointing it out X-Git-Tag: v0.009012~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7b8177f835014e871d1c75016272ba320f4cd975;p=gitmo%2FRole-Tiny.git also update Role::Tiny to handle VSTRING and credit doy for pointing it out --- diff --git a/Changes b/Changes index 5a79991..a2aec8d 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,5 @@ - fix bug where constants containing a reference weren't handled correctly - (ref(\[]) is 'REF' not 'SCALAR') + (ref(\[]) is 'REF' not 'SCALAR', ref(\v1) is 'VSTRING') 0.009011 - 2011-10-03 - add support for DEMOLISH diff --git a/lib/Moo.pm b/lib/Moo.pm index 023a460..f7633cd 100644 --- a/lib/Moo.pm +++ b/lib/Moo.pm @@ -461,6 +461,8 @@ chip - Chip Salzenberg (cpan:CHIPS) ajgb - Alex J. G. Burzyński (cpan:AJGB) +doy - Jesse Luehrs (cpan:DOY) + =head1 COPYRIGHT Copyright (c) 2010-2011 the Moo L and L diff --git a/lib/Role/Tiny.pm b/lib/Role/Tiny.pm index 45d8b03..4479012 100644 --- a/lib/Role/Tiny.pm +++ b/lib/Role/Tiny.pm @@ -22,6 +22,11 @@ sub _load_module { return 1; } +{ # \[] is REF, not SCALAR. \v1 is VSTRING (thanks to doy for that one) + my %reftypes = map +($_ => 1), qw(SCALAR REF VSTRING); + sub _is_scalar_ref { $reftypes{ref($_[0])} } +} + sub import { my $target = caller; my $me = shift; @@ -43,12 +48,12 @@ sub import { die "Only one role supported at a time by with" if @_ > 1; $me->apply_role_to_package($target, $_[0]); }; - # grab all *non-constant* (ref eq 'SCALAR'/'REF') subs present + # grab all *non-constant* (stash slot is not a scalarref) subs present # in the symbol table and store their refaddrs (no need to forcibly # inflate constant subs into real subs) - also add '' to here (this # is used later) @{$INFO{$target}{not_methods}={}}{ - '', map { *$_{CODE}||() } grep !(ref =~ /^(?:SCALAR|REF)$/), values %$stash + '', map { *$_{CODE}||() } grep !_is_scalar_ref($_), values %$stash } = (); # a role does itself $APPLIED_TO{$target} = { $target => undef }; @@ -177,7 +182,7 @@ sub _concrete_methods_of { my $code = *{$stash->{$_}}{CODE}; # rely on the '' key we added in import for "no code here" exists $not_methods->{$code||''} ? () : ($_ => $code) - } grep !(ref($stash->{$_}) =~ /^(?:SCALAR|REF)$/), keys %$stash + } grep !_is_scalar_ref($stash->{$_}), keys %$stash }; }; } @@ -201,7 +206,7 @@ sub _install_methods { # determine already extant methods of target my %has_methods; @has_methods{grep - +((ref($stash->{$_}) =~ /^(?:SCALAR|REF)$/) || (*{$stash->{$_}}{CODE})), + +(_is_scalar_ref($stash->{$_}) || *{$stash->{$_}}{CODE}), keys %$stash } = (); diff --git a/t/role-tiny.t b/t/role-tiny.t index 7bbe99c..ebd7b8e 100644 --- a/t/role-tiny.t +++ b/t/role-tiny.t @@ -21,6 +21,7 @@ BEGIN { use constant SIMPLE => 'simple'; use constant REF_CONST => [ 'ref_const' ]; + use constant VSTRING_CONST => v1; sub req1 { } sub req2 { }