« blib/lib/Perl/Critic/Policy/InputOutput/ProhibitExplicitStdin.pm
↑ Summary
blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitUniversalIsa.pm »
| 1 | package Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalCan; | ||||||||||
| 2 | |||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities :classification hashify }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 40 | our $VERSION = '1.150'; | |||||||||
| 12 | |||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||
| 14 | |||||||||||
| 15 | 40 | Readonly::Scalar my $DESC => q{UNIVERSAL::can should not be used as a function}; | |||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => q{Use eval{$obj->can($pkg)} instead}; ## no critic (RequireInterp); | |||||||||
| 17 | |||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||
| 19 | |||||||||||
| 20 | 90 | ▶ | sub supported_parameters { return () } | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 21 | 75 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 22 | 74 | ▶ | sub default_themes { return qw( core maintenance certrule ) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 23 | 31 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | |||||||||||
| 25 | #----------------------------------------------------------------------------- | ||||||||||
| 26 | |||||||||||
| 27 | sub violates { | ||||||||||
| 28 | 332 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||
|
Subroutine
violates: called CC=3 SCAR=15.1
Pod
documented
| |||||||||||
| 29 | |||||||||||
| 30 | 332 | state $isa_can = { hashify( qw( can UNIVERSAL::can ) ) }; | |||||||||
| 31 | 332 | ▶ | return if !$isa_can->{$elem}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | ▶ | return if ! is_function_call($elem); # this also permits 'use UNIVERSAL::can;' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | |||||||||||
| 34 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | |||||||||||
| 38 | 40 | 1; | |||||||||
| 39 | |||||||||||
| 40 | __END__ | ||||||||||
| 41 | |||||||||||
| 42 | #----------------------------------------------------------------------------- | ||||||||||
| 43 | |||||||||||
| 44 | =pod | ||||||||||
| 45 | |||||||||||
| 46 | =head1 NAME | ||||||||||
| 47 | |||||||||||
| 48 | Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalCan - Write C<< eval { $foo->can($name) } >> instead of C<UNIVERSAL::can($foo, $name)>. | ||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | =head1 AFFILIATION | ||||||||||
| 52 | |||||||||||
| 53 | This Policy is part of the core L<Perl::Critic|Perl::Critic> | ||||||||||
| 54 | distribution. | ||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | =head1 DESCRIPTION | ||||||||||
| 58 | |||||||||||
| 59 | print UNIVERSAL::can($obj, 'Foo::Bar') ? 'yes' : 'no'; #not ok | ||||||||||
| 60 | print eval { $obj->can('Foo::Bar') } ? 'yes' : 'no'; #ok | ||||||||||
| 61 | |||||||||||
| 62 | As of Perl 5.9.3, the use of UNIVERSAL::can as a function has been | ||||||||||
| 63 | deprecated and the method form is preferred instead. Formerly, the | ||||||||||
| 64 | functional form was recommended because it gave valid results even | ||||||||||
| 65 | when the object was C<undef> or an unblessed scalar. However, the | ||||||||||
| 66 | functional form makes it impossible for packages to override C<can()>, | ||||||||||
| 67 | a technique which is crucial for implementing mock objects and some | ||||||||||
| 68 | facades. | ||||||||||
| 69 | |||||||||||
| 70 | See L<UNIVERSAL::can|UNIVERSAL::can> for a more thorough discussion of | ||||||||||
| 71 | this topic. | ||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | =head1 CONFIGURATION | ||||||||||
| 75 | |||||||||||
| 76 | This Policy is not configurable except for the standard options. | ||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | =head1 SEE ALSO | ||||||||||
| 80 | |||||||||||
| 81 | L<Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalIsa|Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalIsa> | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | =head1 AUTHOR | ||||||||||
| 85 | |||||||||||
| 86 | Chris Dolan <cdolan@cpan.org> | ||||||||||
| 87 | |||||||||||
| 88 | |||||||||||
| 89 | =head1 COPYRIGHT | ||||||||||
| 90 | |||||||||||
| 91 | Copyright (c) 2006-2023 Chris Dolan. | ||||||||||
| 92 | |||||||||||
| 93 | This program is free software; you can redistribute it and/or modify | ||||||||||
| 94 | it under the same terms as Perl itself. The full text of this license | ||||||||||
| 95 | can be found in the LICENSE file included with this module. | ||||||||||
| 96 | |||||||||||
| 97 | =cut | ||||||||||
| 98 | |||||||||||
| 99 | # Local Variables: | ||||||||||
| 100 | # mode: cperl | ||||||||||
| 101 | # cperl-indent-level: 4 | ||||||||||
| 102 | # fill-column: 78 | ||||||||||
| 103 | # indent-tabs-mode: nil | ||||||||||
| 104 | # c-indentation-style: bsd | ||||||||||
| 105 | # End: | ||||||||||
| 106 | # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : | ||||||||||