line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen; |
2
|
|
|
|
|
|
|
|
3
|
40
|
|
|
40
|
|
27368
|
use 5.010001; |
|
40
|
|
|
|
|
188
|
|
4
|
40
|
|
|
40
|
|
243
|
use strict; |
|
40
|
|
|
|
|
146
|
|
|
40
|
|
|
|
|
858
|
|
5
|
40
|
|
|
40
|
|
229
|
use warnings; |
|
40
|
|
|
|
|
124
|
|
|
40
|
|
|
|
|
1008
|
|
6
|
|
|
|
|
|
|
|
7
|
40
|
|
|
40
|
|
241
|
use Readonly; |
|
40
|
|
|
|
|
123
|
|
|
40
|
|
|
|
|
2003
|
|
8
|
|
|
|
|
|
|
|
9
|
40
|
|
|
40
|
|
278
|
use version; |
|
40
|
|
|
|
|
150
|
|
|
40
|
|
|
|
|
401
|
|
10
|
|
|
|
|
|
|
|
11
|
40
|
|
|
40
|
|
3937
|
use Perl::Critic::Utils qw{ :severities :classification :ppi }; |
|
40
|
|
|
|
|
153
|
|
|
40
|
|
|
|
|
2201
|
|
12
|
40
|
|
|
40
|
|
16273
|
use parent 'Perl::Critic::Policy'; |
|
40
|
|
|
|
|
184
|
|
|
40
|
|
|
|
|
279
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '1.148'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Readonly::Scalar my $FORK_HANDLES_RX => qr/\A (?: -[|] | [|]- ) \z/xms; |
19
|
|
|
|
|
|
|
Readonly::Scalar my $DESC => q{Two-argument "open" used}; |
20
|
|
|
|
|
|
|
Readonly::Scalar my $EXPL => [ 207 ]; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Readonly::Scalar my $MINIMUM_VERSION => version->new(5.006); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
25
|
|
|
|
|
|
|
|
26
|
98
|
|
|
98
|
0
|
1672
|
sub supported_parameters { return () } |
27
|
103
|
|
|
103
|
1
|
412
|
sub default_severity { return $SEVERITY_HIGHEST } |
28
|
92
|
|
|
92
|
1
|
448
|
sub default_themes { return qw(core pbp bugs security certrule) } |
29
|
45
|
|
|
45
|
1
|
188
|
sub applies_to { return 'PPI::Token::Word' } |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub violates { |
34
|
468
|
|
|
468
|
1
|
933
|
my ($self, $elem, $document) = @_; |
35
|
|
|
|
|
|
|
|
36
|
468
|
100
|
|
|
|
989
|
return if $elem->content() ne 'open'; |
37
|
59
|
100
|
|
|
|
315
|
return if ! is_function_call($elem); |
38
|
|
|
|
|
|
|
|
39
|
58
|
|
|
|
|
208
|
my $version = $document->highest_explicit_perl_version(); |
40
|
58
|
100
|
100
|
|
|
174
|
return if $version and $version < $MINIMUM_VERSION; |
41
|
|
|
|
|
|
|
|
42
|
57
|
|
|
|
|
176
|
my @args = parse_arg_list($elem); |
43
|
|
|
|
|
|
|
|
44
|
57
|
100
|
|
|
|
151
|
if ( scalar @args <= 2 ) { |
45
|
34
|
100
|
66
|
|
|
239
|
return if @args == 2 |
|
|
|
100
|
|
|
|
|
46
|
|
|
|
|
|
|
&& $args[1]->[0]->isa( 'PPI::Token::Quote' ) |
47
|
|
|
|
|
|
|
&& $args[1]->[0]->string() =~ $FORK_HANDLES_RX; |
48
|
29
|
|
|
|
|
353
|
return $self->violation( $DESC, $EXPL, $elem ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
23
|
|
|
|
|
94
|
return; # ok! |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
1; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
__END__ |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=pod |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=for stopwords perlipc |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 NAME |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen - Write C<< open $fh, q{<}, $filename; >> instead of C<< open $fh, "<$filename"; >>. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 AFFILIATION |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
This Policy is part of the core L<Perl::Critic|Perl::Critic> |
71
|
|
|
|
|
|
|
distribution. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 DESCRIPTION |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
The three-argument form of C<open> (introduced in Perl 5.6) prevents |
77
|
|
|
|
|
|
|
subtle bugs that occur when the filename starts with funny characters |
78
|
|
|
|
|
|
|
like '>' or '<'. The L<IO::File|IO::File> module provides a nice |
79
|
|
|
|
|
|
|
object-oriented interface to filehandles, which I think is more |
80
|
|
|
|
|
|
|
elegant anyway. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
open( $fh, '>output.txt' ); # not ok |
83
|
|
|
|
|
|
|
open( $fh, q{>}, 'output.txt' ); # ok |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
use IO::File; |
86
|
|
|
|
|
|
|
my $fh = IO::File->new( 'output.txt', q{>} ); # even better! |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
It's also more explicitly clear to define the input mode of the file, |
89
|
|
|
|
|
|
|
as in the difference between these two: |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
open( $fh, 'foo.txt' ); # BAD: Reader must think what default mode is |
92
|
|
|
|
|
|
|
open( $fh, '<', 'foo.txt' ); # GOOD: Reader can see open mode |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
There is also a one-argument form of C<open> which retrieves the expression to |
95
|
|
|
|
|
|
|
open from the global variable with the same name as the handle, but this has |
96
|
|
|
|
|
|
|
the same problems as the two-argument form, and adds in more ambiguity. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
our $FH = '<foo.txt'; |
99
|
|
|
|
|
|
|
open( FH ); # not ok |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
This policy will not complain if the file explicitly states that it is |
102
|
|
|
|
|
|
|
compatible with a version of perl prior to 5.6 via an include |
103
|
|
|
|
|
|
|
statement, e.g. by having C<require 5.005> in it. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 CONFIGURATION |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
This Policy is not configurable except for the standard options. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 NOTES |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
There is one case in which you are forced to use the two-argument form of |
114
|
|
|
|
|
|
|
open: when doing a safe pipe open, as described in L<perlipc|perlipc>. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 SEE ALSO |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
L<IO::Handle|IO::Handle> |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
L<IO::File|IO::File> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 AUTHOR |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 COPYRIGHT |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Copyright (c) 2005-2021 Imaginative Software Systems. 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. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# Local Variables: |
136
|
|
|
|
|
|
|
# mode: cperl |
137
|
|
|
|
|
|
|
# cperl-indent-level: 4 |
138
|
|
|
|
|
|
|
# fill-column: 78 |
139
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
140
|
|
|
|
|
|
|
# c-indentation-style: bsd |
141
|
|
|
|
|
|
|
# End: |
142
|
|
|
|
|
|
|
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : |