« blib/lib/Perl/Critic/Policy/Documentation/PodSpelling.pm
↑ Summary
blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitEscapedMetacharacters.pm »
| 1 | package Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities :classification :ppi }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 40 | our $VERSION = '1.150'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 40 | Readonly::Scalar my $FORK_HANDLES_RX => qr/\A (?: -[|] | [|]- ) \z/xms; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 40 | Readonly::Scalar my $DESC => q{Two-argument "open" used}; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 40 | Readonly::Scalar my $EXPL => [ 207 ]; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 40 | Readonly::Scalar my $MINIMUM_VERSION => version->new(5.006); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 90 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 75 | ▶ | sub default_severity { return $SEVERITY_HIGHEST } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 92 | ▶ | sub default_themes { return qw(core pbp bugs security certrule) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 37 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | sub violates { | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 361 | ▶ | my ($self, $elem, $document) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=9 SCAR=42.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 361 | ▶ | return if $elem->content() ne 'open'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | return if ! is_function_call($elem); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $version = $document->highest_explicit_perl_version(); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | return if $version and $version < $MINIMUM_VERSION; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$version and $version < $MINIMUM_VERSION
MC/DC0%0/2
$version and $version < $MINIMUM_VERSION
$version
$version < $MINIMUM_VERSION
Truth table0%0/3
A and B
A= $versionB= $version < $MINIMUM_VERSION
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my @args = parse_arg_list($elem); | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ( scalar @args <= 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return if @args == 2 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
@args == 2 and $args[1][0]->isa("PPI::Token::Quote") and $args[1][0]->string =~ /$FORK_HANDLES_RX/
@args == 2 and $args[1][0]->isa("PPI::Token::Quote")
MC/DC0%0/3
@args == 2 and $args[1][0]->isa("PPI::Token::Quote") and $args[1][0]->string =~ /$FORK_HANDLES_RX/
@args == 2
$args[1][0]->isa("PPI::Token::Quote")
$args[1][0]->string =~ /$FORK_HANDLES_RX/
Truth table0%0/4
A and B and C
A= @args == 2B= $args[1][0]->isa("PPI::Token::Quote")C= $args[1][0]->string =~ /$FORK_HANDLES_RX/
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | && $args[1]->[0]->isa( 'PPI::Token::Quote' ) | ||||||||||||||||||||||||||||||||||||||||||
| 47 | && $args[1]->[0]->string() =~ $FORK_HANDLES_RX; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return; # ok! | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 40 | 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 : | ||||||||||||||||||||||||||||||||||||||||||