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 / ProhibitTwoArgOpen.pm
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) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13
14 use Readonly;
15
16 use version;
17
18 use Perl::Critic::Utils qw{ :severities :classification :ppi };
19 use base 'Perl::Critic::Policy';
20
21 our $VERSION = '1.088';
22
23 #-----------------------------------------------------------------------------
24
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 ];
28
29 Readonly::Scalar my $MINIMUM_VERSION => version->new(5.006);
30
31 #-----------------------------------------------------------------------------
32
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'         }
37
38 #-----------------------------------------------------------------------------
39
40 sub violates {
41     my ($self, $elem, $document) = @_;
42
43     return if $elem ne 'open';
44     return if ! is_function_call($elem);
45
46     my $version = $document->highest_explicit_perl_version();
47     return if $version and $version < $MINIMUM_VERSION;
48
49     my @args = parse_arg_list($elem);
50
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 );
56     }
57
58     return; # ok!
59 }
60
61 1;
62
63 __END__
64
65 #-----------------------------------------------------------------------------
66
67 =pod
68
69 =head1 NAME
70
71 Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen - Write C<< open $fh, q{<}, $filename; >> instead of C<< open $fh, "<$filename"; >>.
72
73 =head1 AFFILIATION
74
75 This Policy is part of the core L<Perl::Critic> distribution.
76
77
78 =head1 DESCRIPTION
79
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
84 elegant anyway.
85
86   open( $fh, '>output.txt' );          # not ok
87   open( $fh, q{>}, 'output.txt' );     # ok
88
89   use IO::File;
90   my $fh = IO::File->new( 'output.txt', q{>} ); # even better!
91
92 It's also more explicitly clear to define the input mode of the
93 file, as in the difference between these two:
94
95   open( $fh, 'foo.txt' );       # BAD: Reader must think what default mode is
96   open( $fh, '<', 'foo.txt' );  # GOOD: Reader can see open mode
97
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.
101
102
103 =head1 CONFIGURATION
104
105 This Policy is not configurable except for the standard options.
106
107
108 =head1 NOTES
109
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
112 that loophole.
113
114 =head1 SEE ALSO
115
116 L<IO::Handle>
117
118 L<IO::File>
119
120 =head1 AUTHOR
121
122 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
123
124 =head1 COPYRIGHT
125
126 Copyright (C) 2005-2007 Jeffrey Ryan Thalhammer.  All rights reserved.
127
128 This program is free software; you can redistribute it and/or modify
129 it under the same terms as Perl itself.
130
131 =cut
132
133 # Local Variables:
134 #   mode: cperl
135 #   cperl-indent-level: 4
136 #   fill-column: 78
137 #   indent-tabs-mode: nil
138 #   c-indentation-style: bsd
139 # End:
140 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :