1 package Perl::Critic::Policy::Dynamic::NoIndirect;
10 Perl::Critic::Policy::Dynamic::NoIndirect - Perl::Critic policy against indirect method calls.
18 our $VERSION = '0.05';
22 This L<Perl::Critic> dynamic policy reports any use of indirect object syntax with a C<'stern'> severity.
23 It's listed under the C<'dynamic'> and C<'maintenance'> themes.
25 Since it wraps around L<indirect>, it needs to compile the audited code and as such is implemented as a subclass of L<Perl::Critic::DynamicPolicy>.
29 use base qw/Perl::Critic::DynamicPolicy/;
31 use Perl::Critic::Utils qw/:severities/;
33 sub default_severity { $SEVERITY_HIGH }
34 sub default_themes { qw/dynamic maintenance/ }
35 sub applies_to { 'PPI::Document' }
39 $obj = '{' if $obj =~ /^\s*\{/;
43 sub violates_dynamic {
44 my ($self, undef, $doc) = @_;
47 if ($doc->isa('PPI::Document::File')) {
48 $file = $doc->filename;
49 open my $fh, '<', $file
50 or do { require Carp; Carp::confess("Can't open $file for reading: $!") };
51 $src = do { local $/; <$fh> };
54 $src = $doc->serialize;
57 $file =~ s/(?<!\\)((\\\\)*)"/$1\\"/g;
60 my $hook = sub { push @errs, [ @_ ] };
62 my $wrapper = <<" WRAPPER";
67 no indirect hook => \$hook;
76 eval $wrapper; ## no critic
79 Carp::croak("Couldn't compile the source wrapper: $err");
88 my ($obj, $meth, $line) = @$_[0, 1, 3];
89 my $tag = join "\0", $line, $meth, $tag_obj->($obj);
90 push @{$errs_tags{$tag}}, [ $obj, $meth ];
95 my $pos = $elt->location;
98 my $tag = join "\0", $pos->[0], $elt, $tag_obj->($elt->snext_sibling);
99 if (my $errs = $errs_tags{$tag}) {
100 push @violations, do { my $e = pop @$errs; push @$e, $elt; $e };
101 delete $errs_tags{$tag} unless @$errs;
102 return 1 unless %errs_tags;
110 my ($obj, $meth, $elt) = @$_;
111 $obj = ($obj =~ /^\s*\{/) ? "a block" : "object \"$obj\"";
113 "Indirect call of method \"$meth\" on $obj",
114 "You really wanted $obj\->$meth",
122 The uses of the L<indirect> pragma inside the audited code take precedence over this policy.
123 Hence no violations will be reported for indirect method calls that are located inside the lexical scope of C<use indirect> or C<< no indirect hook => ... >>.
124 Occurrences of C<no indirect> won't be a problem.
126 Since the reports generated by L<indirect> are remapped to the corresponding L<PPI::Element> objects, the order in which the violations are returned is different from the order given by L<indirect> : the former is the document order (top to bottom, left to right) while the latter is the optree order (arguments before function calls).
130 L<perl> 5.8, L<Carp>.
132 L<Perl::Critic>, L<Perl::Critic::Dynamic>.
138 L<Perl::Critic::Policy::Objects::ProhibitIndirectSyntax> is a L<Perl::Critic> policy that statically checks for indirect constructs.
139 But to be static it has to be very restricted : you have to manually specify which subroutine names are methods for which the indirect form should be forbidden.
140 This can lead to false positives (a subroutine with the name you gave is defined in the current scope) and negatives (indirect constructs for methods you didn't specify).
141 But you don't need to actually compile (or run, as it's more or less the same thing) the code.
145 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
147 You can contact me by mail or on C<irc.perl.org> (vincent).
151 Please report any bugs or feature requests to C<bug-perl-critic-policy-dynamic-noindirect at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Perl-Critic-Policy-Dynamic-NoIndirect>.
152 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
156 You can find documentation for this module with the perldoc command.
158 perldoc Perl::Critic::Policy::Dynamic::NoIndirect
160 =head1 COPYRIGHT & LICENSE
162 Copyright 2009,2010 Vincent Pit, all rights reserved.
164 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
168 1; # End of Perl::Critic::Policy::Dynamic::NoIndirect