1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitUniversalCan.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalCan;
15 use Perl::Critic::Utils qw{ :severities :classification };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{UNIVERSAL::can should not be used as a function};
23 Readonly::Scalar my $EXPL => q{Use eval{$obj->can($pkg)} instead}; ##no critic 'RequireInterp';
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_MEDIUM }
29 sub default_themes { return qw( core maintenance ) }
30 sub applies_to { return 'PPI::Token::Word' }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
36 return if !($elem eq 'can' || $elem eq 'UNIVERSAL::can');
37 return if ! is_function_call($elem); # this also permits 'use UNIVERSAL::can;'
39 return $self->violation( $DESC, $EXPL, $elem );
47 #-----------------------------------------------------------------------------
53 Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalCan - Write C<< eval { $foo->can($name) } >> instead of C<UNIVERSAL::can($foo, $name)>.
57 This Policy is part of the core L<Perl::Critic> distribution.
62 print UNIVERSAL::can($obj, 'Foo::Bar') ? 'yes' : 'no'; #not ok
63 print eval { $obj->can('Foo::Bar') } ? 'yes' : 'no'; #ok
65 As of Perl 5.9.3, the use of UNIVERSAL::can as a function has been
66 deprecated and the method form is preferred instead. Formerly, the
67 functional form was recommended because it gave valid results even
68 when the object was C<undef> or an unblessed scalar. However, the
69 functional form makes it impossible for packages to override C<can()>,
70 a technique which is crucial for implementing mock objects and some
73 See the CPAN module L<UNIVERSAL::can> for a more thorough discussion
79 This Policy is not configurable except for the standard options.
84 L<Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalIsa>
88 Chris Dolan <cdolan@cpan.org>
92 Copyright (c) 2006-2008 Chris Dolan. All rights reserved.
94 This program is free software; you can redistribute it and/or modify
95 it under the same terms as Perl itself. The full text of this license
96 can be found in the LICENSE file included with this module.
102 # cperl-indent-level: 4
104 # indent-tabs-mode: nil
105 # c-indentation-style: bsd
107 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :