Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / InputOutput / ProhibitBacktickOperators.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/InputOutput/ProhibitBacktickOperators.pm $
3 #     $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::InputOutput::ProhibitBacktickOperators;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities is_in_void_context };
16
17 use base 'Perl::Critic::Policy';
18
19 our $VERSION = '1.088';
20
21 #-----------------------------------------------------------------------------
22
23 Readonly::Scalar my $EXPL => q{Use IPC::Open3 instead};
24 Readonly::Scalar my $DESC => q{Backtick operator used};
25
26 Readonly::Scalar my $VOID_EXPL => q{Assign result to a variable or use system() instead};
27 Readonly::Scalar my $VOID_DESC => q{Backtick operator used in void context};
28
29 #-----------------------------------------------------------------------------
30
31 sub supported_parameters {
32     return (
33         {
34             name        => 'only_in_void_context',
35             description => 'Allow backticks everywhere except in void contexts.',
36             behavior    => 'boolean',
37         },
38     );
39 }
40
41 sub default_severity { return $SEVERITY_MEDIUM }
42 sub default_themes   { return qw(core maintenance)   }
43 sub applies_to       { return qw(PPI::Token::QuoteLike::Backtick
44                                  PPI::Token::QuoteLike::Command ) }
45
46 #-----------------------------------------------------------------------------
47
48 sub violates {
49     my ( $self, $elem, undef ) = @_;
50
51     if ( $self->{_only_in_void_context} ) {
52         return if not is_in_void_context( $elem );
53
54         return $self->violation( $VOID_DESC, $VOID_EXPL, $elem );
55     }
56
57     return $self->violation( $DESC, $EXPL, $elem );
58 }
59
60 1;
61
62 __END__
63
64 #-----------------------------------------------------------------------------
65
66 =pod
67
68 =head1 NAME
69
70 Perl::Critic::Policy::InputOutput::ProhibitBacktickOperators - Discourage stuff like C<@files = `ls $directory`>.
71
72 =head1 AFFILIATION
73
74 This Policy is part of the core L<Perl::Critic> distribution.
75
76
77 =head1 DESCRIPTION
78
79 Backticks are super-convenient, especially for CGI programs, but I
80 find that they make a lot of noise by filling up STDERR with messages
81 when they fail.  I think its better to use IPC::Open3 to trap all the
82 output and let the application decide what to do with it.
83
84   use IPC::Open3 'open3';
85   $SIG{CHLD} = 'IGNORE';
86
87   @output = `some_command`;                      #not ok
88
89   my ($writer, $reader, $err);
90   open3($writer, $reader, $err, 'some_command'); #ok;
91   @output = <$reader>;  #Output here
92   @errors = <$err>;     #Errors here, instead of the console
93
94
95 =head1 CONFIGURATION
96
97 Alternatively, if you do want to use backticks, you can restrict
98 checks to void contexts by adding the following to your
99 F<.perlcriticrc> file:
100
101   [InputOutput::ProhibitBacktickOperators]
102   only_in_void_context = 1
103
104 The purpose of backticks is to capture the output of an external
105 command.  Use of them in a void context is likely a bug.  If the
106 output isn't actually required, C<system()> should be used.  Otherwise
107 assign the result to a variable.
108
109   `some_command`;                      #not ok
110   $output = `some_command`;            #ok
111   @output = `some_command`;            #ok
112
113
114 =head1 NOTES
115
116 This policy also prohibits the generalized form of backticks seen as
117 C<qx{}>.
118
119 See L<perlipc> for more discussion on using C<wait()> instead of
120 C<$SIG{CHLD} = 'IGNORE'>.
121
122 =head1 AUTHOR
123
124 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
125
126 =head1 COPYRIGHT
127
128 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
129
130 This program is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself.  The full text of this license
132 can be found in the LICENSE file included with this module.
133
134 =cut
135
136 # Local Variables:
137 #   mode: cperl
138 #   cperl-indent-level: 4
139 #   fill-column: 78
140 #   indent-tabs-mode: nil
141 #   c-indentation-style: bsd
142 # End:
143 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :