File Coverage

blib/lib/Perl/Critic/Policy/Community/IndirectObjectNotation.pm
Criterion Covered Total %
statement 13 14 92.8
branch n/a
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 20 22 90.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Community::IndirectObjectNotation;
2              
3 1     1   933 use strict;
  1         3  
  1         43  
4 1     1   7 use warnings;
  1         1  
  1         63  
5              
6 1     1   7 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         2  
  1         75  
7 1     1   514 use parent 'Perl::Critic::Policy::Objects::ProhibitIndirectSyntax';
  1         3  
  1         5  
8              
9             our $VERSION = 'v1.0.4';
10              
11 4     4 1 30379 sub default_severity { $SEVERITY_HIGHEST }
12 0     0 1   sub default_themes { 'community' }
13              
14             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>