File Coverage

blib/lib/Perl/Critic/Policy/Community/BarewordFilehandles.pm
Criterion Covered Total %
statement 31 32 96.8
branch 4 4 100.0
condition 6 8 75.0
subroutine 10 11 90.9
pod 4 5 80.0
total 55 60 91.6


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   404 use warnings;
  1         2  
  1         24  
4 1     1   4  
  1         2  
  1         23  
5             use Perl::Critic::Utils qw(:severities :classification :ppi);
6 1     1   5 use parent 'Perl::Critic::Policy';
  1         1  
  1         42  
7 1     1   305  
  1         2  
  1         4  
8             our $VERSION = 'v1.0.3';
9              
10             use constant DESC => 'Using bareword filehandles';
11 1     1   68 use constant EXPL => 'Bareword filehandles are a legacy feature, creating the filehandles as package variables. Use lexical, scoped filehandles instead (open my $fh, ...).';
  1         2  
  1         68  
12 1     1   5  
  1         2  
  1         265  
13              
14 6     6 0 75480 my %openers = (
15 18     18 1 241 accept => 1,
16 0     0 1 0 open => 1,
17 6     6 1 203223 opendir => 1,
18             pipe => 2,
19             socket => 1,
20             socketpair => 2,
21             );
22              
23             my %builtins = (
24             ARGV => 1,
25             ARGVOUT => 1,
26             DATA => 1,
27             STDERR => 1,
28             STDIN => 1,
29             STDOUT => 1,
30             );
31              
32             my ($self, $elem) = @_;
33             return () unless exists $openers{$elem} and is_function_call $elem;
34             my $num_handles = $openers{$elem};
35            
36             my @args = parse_arg_list $elem;
37             my @handles = splice @args, 0, $num_handles;
38 229     229 1 1601
39 229 100 66     361 my @violations;
40 90         18020 foreach my $handle (@handles) {
41             my $name = pop @$handle // next;
42 90         361 push @violations, $self->violation(DESC, EXPL, $elem)
43 90         13829 if $name->isa('PPI::Token::Word') and !exists $builtins{$name};
44             }
45 90         116
46 90         140 return @violations;
47 97   50     742 }
48              
49 97 100 100     300 1;
50              
51             =head1 NAME
52 90         2679  
53             Perl::Critic::Policy::Community::BarewordFilehandles - Don't use bareword
54             filehandles other than built-ins
55              
56             =head1 DESCRIPTION
57              
58             Bareword filehandles are allowed in C<open()> as a legacy feature, but will use
59             a global package variable. Instead, use a lexical variable with C<my> so that
60             the filehandle is scoped to the current block, and will be automatically closed
61             when it goes out of scope. Built-in bareword filehandles like C<STDOUT> and
62             C<DATA> are ok.
63              
64             open FH, '<', $filename; # not ok
65             open my $fh, '<', $filename; # ok
66              
67             This policy is similar to the core policy
68             L<Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles>, but allows
69             more combinations of built-in bareword handles and filehandle-opening functions
70             such as C<pipe> and C<socketpair>.
71              
72             =head1 AFFILIATION
73              
74             This policy is part of L<Perl::Critic::Community>.
75              
76             =head1 CONFIGURATION
77              
78             This policy is not configurable except for the standard options.
79              
80             =head1 AUTHOR
81              
82             Dan Book, C<dbook@cpan.org>
83              
84             =head1 COPYRIGHT AND LICENSE
85              
86             Copyright 2015, Dan Book.
87              
88             This library is free software; you may redistribute it and/or modify it under
89             the terms of the Artistic License version 2.0.
90              
91             =head1 SEE ALSO
92              
93             L<Perl::Critic>, L<bareword::filehandles>