Policy/Community/BarewordFilehandles.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 100% 4 / 4 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 90.9% 10 / 11 Podpod 80.0% 4 / 5 totaltotal 89.8% 62 / 69 CC 7 SCAR 12.7
CC
7
Cov
94%
violates
7.0
default_themes
2.0
BEGIN
1.0
CRAP 3.5
« Policy/Community/OpenArgs.pm ↑ Summary Policy/Community/POSIXImports.pm »
1 package Perl::Critic::Policy::Community::BarewordFilehandles;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Perl::Critic::Utils qw(:severities :classification :ppi);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 'v1.0.4';
10  
11 1 ▶use constant DESC => 'Using bareword filehandles';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant EXPL => 'Bareword filehandles are a legacy feature, creating the filehandles as package variables. Use lexical, scoped filehandles instead (open my $fh, ...).';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶sub supported_parameters { () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
15 18 ▶sub default_severity { $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
16 0 ▶sub default_themes { 'community' }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
17 6 ▶sub applies_to { 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
18  
19 1 my %openers = (
20 accept => 1,
21 open => 1,
22 opendir => 1,
23 pipe => 2,
24 socket => 1,
25 socketpair => 2,
26 );
27  
28 1 my %builtins = (
29 ARGV => 1,
30 ARGVOUT => 1,
31 DATA => 1,
32 STDERR => 1,
33 STDIN => 1,
34 STDOUT => 1,
35 );
36  
37 229 ▶sub violates {
Subroutine violates: called CC=7 SCAR=19.5
Pod documented
38 229 my ($self, $elem) = @_;
39 229 ▶ return () unless exists $openers{$elem} and is_function_call $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $openers{$elem} and is_function_call($elem) 139 90 229
Condition66%2/3
exists $openers{$elem} and is_function_call($elem)
!ll&&!rl&&r
139090
MC/DC50%1/2
exists $openers{$elem} and is_function_call($elem)
exists $openers{$elem} is_function_call($elem)
Truth table66%2/3
A and B
A= exists $openers{$elem}B= is_function_call($elem)
ABresult
0X0
100
111
40 90 my $num_handles = $openers{$elem};
41
42 90 my @args = parse_arg_list $elem;
43 90 my @handles = splice @args, 0, $num_handles;
44
45 90 my @violations;
46 90 foreach my $handle (@handles) {
47 97 ▶ my $name = pop @$handle // next;
Condition50%1/2
pop @$handle // (next)
l!l
970
MC/DC0%0/1
pop @$handle // (next)
pop @$handle
Truth table50%1/2
A
A= pop @$handle
Aresult
11
00
48 push @violations, $self->violation(DESC, EXPL, $elem)
49 97 ▶ if $name->isa('PPI::Token::Word') and !exists $builtins{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $name->isa("PPI::Token::Word") and not exists $builtins{$name} 18 79 97
Condition100%3/3
$name->isa("PPI::Token::Word") and not exists $builtins{$name}
!ll&&!rl&&r
512818
MC/DC100%2/2
$name->isa("PPI::Token::Word") and not exists $builtins{$name}
$name->isa("PPI::Token::Word") not exists $builtins{$name}
Truth table100%3/3
A and not(B)
A= $name->isa("PPI::Token::Word")B= not exists $builtins{$name}
ABresult
0X0
100
111
50 }
51
52 90 return @violations;
53 }
54  
55 1 1;
56  
57 =head1 NAME
58  
59 Perl::Critic::Policy::Community::BarewordFilehandles - Don't use bareword
60 filehandles other than built-ins
61  
62 =head1 DESCRIPTION
63  
64 Bareword filehandles are allowed in C<open()> as a legacy feature, but will use
65 a global package variable. Instead, use a lexical variable with C<my> so that
66 the filehandle is scoped to the current block, and will be automatically closed
67 when it goes out of scope. Built-in bareword filehandles like C<STDOUT> and
68 C<DATA> are ok.
69  
70 open FH, '<', $filename; # not ok
71 open my $fh, '<', $filename; # ok
72  
73 This policy is similar to the core policy
74 L<Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles>, but allows
75 more combinations of built-in bareword handles and filehandle-opening functions
76 such as C<pipe> and C<socketpair>.
77  
78 =head1 AFFILIATION
79  
80 This policy is part of L<Perl::Critic::Community>.
81  
82 =head1 CONFIGURATION
83  
84 This policy is not configurable except for the standard options.
85  
86 =head1 AUTHOR
87  
88 Dan Book, C<dbook@cpan.org>
89  
90 =head1 COPYRIGHT AND LICENSE
91  
92 Copyright 2015, Dan Book.
93  
94 This library is free software; you may redistribute it and/or modify it under
95 the terms of the Artistic License version 2.0.
96  
97 =head1 SEE ALSO
98  
99 L<Perl::Critic>, L<bareword::filehandles>
« Policy/Community/OpenArgs.pm ↑ Summary Policy/Community/POSIXImports.pm »