1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/InputOutput/ProhibitTwoArgOpen.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen;
18 use Perl::Critic::Utils qw{ :severities :classification :ppi };
19 use base 'Perl::Critic::Policy';
21 our $VERSION = '1.088';
23 #-----------------------------------------------------------------------------
25 Readonly::Scalar my $STDIO_HANDLES_RX => qr/\b STD (?: IN | OUT | ERR \b)/mx;
26 Readonly::Scalar my $DESC => q{Two-argument "open" used};
27 Readonly::Scalar my $EXPL => [ 207 ];
29 Readonly::Scalar my $MINIMUM_VERSION => version->new(5.006);
31 #-----------------------------------------------------------------------------
33 sub supported_parameters { return () }
34 sub default_severity { return $SEVERITY_HIGHEST }
35 sub default_themes { return qw(core pbp bugs security) }
36 sub applies_to { return 'PPI::Token::Word' }
38 #-----------------------------------------------------------------------------
41 my ($self, $elem, $document) = @_;
43 return if $elem ne 'open';
44 return if ! is_function_call($elem);
46 my $version = $document->highest_explicit_perl_version();
47 return if $version and $version < $MINIMUM_VERSION;
49 my @args = parse_arg_list($elem);
51 if ( scalar @args == 2 ) {
52 # When opening STDIN, STDOUT, or STDERR, the
53 # two-arg form is the only option you have.
54 return if $args[1]->[0] =~ $STDIO_HANDLES_RX;
55 return $self->violation( $DESC, $EXPL, $elem );
65 #-----------------------------------------------------------------------------
71 Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen - Write C<< open $fh, q{<}, $filename; >> instead of C<< open $fh, "<$filename"; >>.
75 This Policy is part of the core L<Perl::Critic> distribution.
80 The three-argument form of C<open> (introduced in Perl 5.6) prevents
81 subtle bugs that occur when the filename starts with funny characters
82 like '>' or '<'. The L<IO::File> module provides a nice
83 object-oriented interface to filehandles, which I think is more
86 open( $fh, '>output.txt' ); # not ok
87 open( $fh, q{>}, 'output.txt' ); # ok
90 my $fh = IO::File->new( 'output.txt', q{>} ); # even better!
92 It's also more explicitly clear to define the input mode of the
93 file, as in the difference between these two:
95 open( $fh, 'foo.txt' ); # BAD: Reader must think what default mode is
96 open( $fh, '<', 'foo.txt' ); # GOOD: Reader can see open mode
98 This policy will not complain if the file explicitly states that it is
99 compatible with a version of perl prior to 5.6 via an include
100 statement, e.g. by having C<require 5.005> in it.
105 This Policy is not configurable except for the standard options.
110 The only time you should use the two-argument form is when you re-open
111 STDIN, STDOUT, or STDERR. But for now, this Policy doesn't provide
122 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
126 Copyright (C) 2005-2007 Jeffrey Ryan Thalhammer. All rights reserved.
128 This program is free software; you can redistribute it and/or modify
129 it under the same terms as Perl itself.
135 # cperl-indent-level: 4
137 # indent-tabs-mode: nil
138 # c-indentation-style: bsd
140 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :