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) $
6 ##############################################################################
8 package Perl::Critic::Policy::InputOutput::ProhibitBacktickOperators;
15 use Perl::Critic::Utils qw{ :severities is_in_void_context };
17 use base 'Perl::Critic::Policy';
19 our $VERSION = '1.088';
21 #-----------------------------------------------------------------------------
23 Readonly::Scalar my $EXPL => q{Use IPC::Open3 instead};
24 Readonly::Scalar my $DESC => q{Backtick operator used};
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};
29 #-----------------------------------------------------------------------------
31 sub supported_parameters {
34 name => 'only_in_void_context',
35 description => 'Allow backticks everywhere except in void contexts.',
36 behavior => 'boolean',
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 ) }
46 #-----------------------------------------------------------------------------
49 my ( $self, $elem, undef ) = @_;
51 if ( $self->{_only_in_void_context} ) {
52 return if not is_in_void_context( $elem );
54 return $self->violation( $VOID_DESC, $VOID_EXPL, $elem );
57 return $self->violation( $DESC, $EXPL, $elem );
64 #-----------------------------------------------------------------------------
70 Perl::Critic::Policy::InputOutput::ProhibitBacktickOperators - Discourage stuff like C<@files = `ls $directory`>.
74 This Policy is part of the core L<Perl::Critic> distribution.
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.
84 use IPC::Open3 'open3';
85 $SIG{CHLD} = 'IGNORE';
87 @output = `some_command`; #not ok
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
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:
101 [InputOutput::ProhibitBacktickOperators]
102 only_in_void_context = 1
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.
109 `some_command`; #not ok
110 $output = `some_command`; #ok
111 @output = `some_command`; #ok
116 This policy also prohibits the generalized form of backticks seen as
119 See L<perlipc> for more discussion on using C<wait()> instead of
120 C<$SIG{CHLD} = 'IGNORE'>.
124 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
128 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
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.
138 # cperl-indent-level: 4
140 # indent-tabs-mode: nil
141 # c-indentation-style: bsd
143 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :