« Policy/Community/ConditionalDeclarations.pm
↑ Summary
Policy/Community/LexicalForeachIterator.pm »
| 1 | package Perl::Critic::Policy::Community::IndirectObjectNotation; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use parent 'Perl::Critic::Policy::Objects::ProhibitIndirectSyntax'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | our $VERSION = 'v1.0.4'; | |
| 10 | |||
| 11 | 4 | ▶ | sub default_severity { $SEVERITY_HIGHEST } |
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||
| 12 | 0 | ▶ | sub default_themes { 'community' } |
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 13 | |||
| 14 | 1 | 1; | |
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Perl::Critic::Policy::Community::IndirectObjectNotation - Don't call methods | ||
| 19 | indirectly | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Perl allows a form of method call where the method name is first, followed by | ||
| 24 | the invocant (class or object to call the method on), then the argument list. | ||
| 25 | This is an unfortunate legacy syntax that should no longer be used. See | ||
| 26 | L<perlobj/"Indirect Object Syntax"> and L<indirect/"REFERENCES"> for more | ||
| 27 | information. | ||
| 28 | |||
| 29 | my $obj = new My::Class @args; # not ok | ||
| 30 | my $obj = My::Class->new(@args); # ok | ||
| 31 | |||
| 32 | It is difficult to detect indirect object notation by static analysis, so this | ||
| 33 | policy only forbids the C<new> method call by default, as it is highly unlikely | ||
| 34 | to be the name of a standard subroutine call. Consider using the L<indirect> | ||
| 35 | pragma to cause the code to warn or die when indirect object notation is used. | ||
| 36 | |||
| 37 | On Perl 5.32 or newer (or automatically with C<use v5.36> or newer), | ||
| 38 | L<feature/"The 'indirect' feature"> can be disabled to prevent the parser from | ||
| 39 | interpreting syntax as indirect object notation. | ||
| 40 | |||
| 41 | This policy is a subclass of the L<Perl::Critic> core policy | ||
| 42 | L<Perl::Critic::Policy::Objects::ProhibitIndirectSyntax>, and performs the same | ||
| 43 | function but in the C<community> theme. | ||
| 44 | |||
| 45 | =head1 AFFILIATION | ||
| 46 | |||
| 47 | This policy is part of L<Perl::Critic::Community>. | ||
| 48 | |||
| 49 | =head1 CONFIGURATION | ||
| 50 | |||
| 51 | This policy can be configured, in the same way as its parent policy | ||
| 52 | L<Perl::Critic::Policy::Objects::ProhibitIndirectSyntax>, to attempt to forbid | ||
| 53 | additional method names from being called indirectly. Be aware this may lead to | ||
| 54 | false positives as it is difficult to detect indirect object notation by static | ||
| 55 | analysis. The C<new> subroutine is always forbidden in addition to these. | ||
| 56 | |||
| 57 | [Community::IndirectObjectNotation] | ||
| 58 | forbid = create destroy | ||
| 59 | |||
| 60 | =head1 AUTHOR | ||
| 61 | |||
| 62 | Dan Book, C<dbook@cpan.org> | ||
| 63 | |||
| 64 | =head1 COPYRIGHT AND LICENSE | ||
| 65 | |||
| 66 | Copyright 2015, Dan Book. | ||
| 67 | |||
| 68 | This library is free software; you may redistribute it and/or modify it under | ||
| 69 | the terms of the Artistic License version 2.0. | ||
| 70 | |||
| 71 | =head1 SEE ALSO | ||
| 72 | |||
| 73 | L<Perl::Critic>, L<indirect> | ||