« blib/lib/Perl/Critic/Policy/InputOutput/RequireBriefOpen.pm
↑ Summary
blib/lib/Perl/Critic/Policy/Subroutines/RequireArgUnpacking.pm »
| 1 | package Perl::Critic::Policy::InputOutput::ProhibitBarewordDirHandles; | ||||||||||||||||||||||||||||
| 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 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities :classification :ppi }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 40 | our $VERSION = '1.150'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 40 | Readonly::Scalar my $DESC => q{Bareword dir handle opened}; | |||||||||||||||||||||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => [ 202, 204 ]; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 90 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 75 | ▶ | sub default_severity { return $SEVERITY_HIGHEST } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 92 | ▶ | sub default_themes { return qw( core pbp bugs certrec ) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 37 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub violates { | ||||||||||||||||||||||||||||
| 28 | 361 | ▶ | my ($self, $elem, undef) = @_; | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=15 SCAR=53.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 361 | ▶ | return if $elem->content() ne 'opendir'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | return if ! is_function_call($elem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | my $first_arg = ( parse_arg_list($elem) )[0]; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | return if !$first_arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | my $token = $first_arg->[0]; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | return if !$token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( $token->isa('PPI::Token::Word') && $token eq 'local' ) { # handle local *DH | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$token->isa("PPI::Token::Word") and $token eq "local"
MC/DC0%0/2
$token->isa("PPI::Token::Word") and $token eq "local"
$token->isa("PPI::Token::Word")
$token eq "local"
Truth table0%0/3
A and B
A= $token->isa("PPI::Token::Word")B= $token eq "local"
| |||||||||||||||||||||||||||||
| 39 | 0 | $token = $first_arg->[1]; # the token that follows local in the first argument | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | return if !$token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ( $token->isa('PPI::Token::Cast') && $token eq q{\\} ) { # handle \*DH | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$token->isa("PPI::Token::Cast") and $token eq "\\"
MC/DC0%0/2
$token->isa("PPI::Token::Cast") and $token eq "\\"
$token->isa("PPI::Token::Cast")
$token eq "\\"
Truth table0%0/3
A and B
A= $token->isa("PPI::Token::Cast")B= $token eq "\\"
| |||||||||||||||||||||||||||||
| 43 | 0 | $token = $first_arg->[1]; # the token that follows \ in the first argument | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | return if !$token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( $token->isa('PPI::Token::Symbol') ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | return $self->violation($DESC, $EXPL, $elem) if $token =~ m/^[*]/xms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } elsif ( $token->isa('PPI::Token::Word') ) { | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | return $self->violation($DESC, $EXPL, $elem) if $token !~ m/^(?:my|our)$/xms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | return; #ok! | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 40 | 1; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | __END__ | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =pod | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 NAME | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Perl::Critic::Policy::InputOutput::ProhibitBarewordDirHandles - Write C<opendir my $dh, $dirname;> instead of C<opendir DH, $dirname;>. | ||||||||||||||||||||||||||||
| 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 | Using bareword symbols to refer to directory handles is particularly evil | ||||||||||||||||||||||||||||
| 77 | because they are global, and you have no idea if that symbol already | ||||||||||||||||||||||||||||
| 78 | points to some other file or directory handle. You can mitigate some of that risk | ||||||||||||||||||||||||||||
| 79 | by C<local>izing the symbol first, but that's pretty ugly. Since Perl | ||||||||||||||||||||||||||||
| 80 | 5.6, you can use an undefined scalar variable as a lexical reference | ||||||||||||||||||||||||||||
| 81 | to an anonymous file handle or directory handle. Alternatively, see the | ||||||||||||||||||||||||||||
| 82 | L<IO::Handle|IO::Handle> or L<IO::Dir|IO::Dir> | ||||||||||||||||||||||||||||
| 83 | modules for an object-oriented approach. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | opendir DH, $some_dir; #not ok | ||||||||||||||||||||||||||||
| 86 | opendir *DH, $some_dir; #not ok | ||||||||||||||||||||||||||||
| 87 | opendir \*DH, $some_dir; #not ok | ||||||||||||||||||||||||||||
| 88 | opendir local *DH, $some_dir; #not ok | ||||||||||||||||||||||||||||
| 89 | opendir $dh, $some_dir; #ok | ||||||||||||||||||||||||||||
| 90 | opendir my $dh, $some_dir; #ok | ||||||||||||||||||||||||||||
| 91 | opendir our $dh, $some_dir; #ok | ||||||||||||||||||||||||||||
| 92 | opendir local $dh, $some_dir; #ok | ||||||||||||||||||||||||||||
| 93 | my $dh = IO::Dir->new($some_dir); #ok | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | And Perl7 will probably drop support for bareword filehandles. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | This Policy is not configurable except for the standard options. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | L<Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles::Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles> | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | L<IO::Handle|IO::Handle> | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | L<IO::Dir|IO::Dir> | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>, | ||||||||||||||||||||||||||||
| 112 | C<github.com/pali>, C<github.com/raforg> | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Copyright (c) 2005-2011, 2021 Imaginative Software Systems. All rights reserved. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 119 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Local Variables: | ||||||||||||||||||||||||||||
| 124 | # mode: cperl | ||||||||||||||||||||||||||||
| 125 | # cperl-indent-level: 4 | ||||||||||||||||||||||||||||
| 126 | # fill-column: 78 | ||||||||||||||||||||||||||||
| 127 | # indent-tabs-mode: nil | ||||||||||||||||||||||||||||
| 128 | # c-indentation-style: bsd | ||||||||||||||||||||||||||||
| 129 | # End: | ||||||||||||||||||||||||||||
| 130 | # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : | ||||||||||||||||||||||||||||