From: Dave Rolsky Date: Sun, 15 Aug 2010 09:17:57 +0000 (+0200) Subject: Make sure that get_method_list and _get_local_methods handle constants in the namespa... X-Git-Tag: 1.05~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7f9ef61ea70b35f1bf8bfef01bbc21300a4c9095;p=gitmo%2FClass-MOP.git Make sure that get_method_list and _get_local_methods handle constants in the namespace safely --- diff --git a/lib/Class/MOP/Mixin/HasMethods.pm b/lib/Class/MOP/Mixin/HasMethods.pm index b8ea9ff..d568e5a 100644 --- a/lib/Class/MOP/Mixin/HasMethods.pm +++ b/lib/Class/MOP/Mixin/HasMethods.pm @@ -152,7 +152,13 @@ sub get_method_list { my $namespace = $self->namespace; - return grep { *{ $namespace->{$_} }{CODE} && $self->has_method($_) } + # Constants will show up as some sort of reference in the namespace hash + # ref. + return grep { + ! ref $namespace->{$_} + && *{ $namespace->{$_} }{CODE} + && $self->has_method($_) + } keys %{$namespace}; } @@ -164,7 +170,8 @@ sub _get_local_methods { my $namespace = $self->namespace; - return map { $self->get_method($_) } grep { *{ $namespace->{$_} }{CODE} } + return map { $self->get_method($_) } + grep { ! ref $namespace->{$_} && *{ $namespace->{$_} }{CODE} } keys %{$namespace}; } diff --git a/t/003_methods.t b/t/003_methods.t index 547ab5f..7dc6ddb 100644 --- a/t/003_methods.t +++ b/t/003_methods.t @@ -352,4 +352,30 @@ is( $new_method->original_method, $method, } } +{ + package HasConstants; + + use constant FOO => 1; + use constant BAR => []; + use constant BAZ => {}; + + sub quux {1} + sub thing {1} +} + +my $HC = Class::MOP::Class->initialize('HasConstants'); + +is_deeply( + [ sort $HC->get_method_list ], + [qw( quux thing )], + 'get_method_list handles constants properly' +); + +is_deeply( + [ sort map { $_->name } $HC->_get_local_methods ], + [qw( quux thing )], + '_get_local_methods handles constants properly' +); + + done_testing;