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             package Perl::Critic::Policy::Community::BarewordFilehandles;
2              
3 1     1   489 use strict;
  1         3  
  1         30  
4 1     1   6 use warnings;
  1         3  
  1         30  
5              
6 1     1   8 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         3  
  1         98  
7 1     1   516 use parent 'Perl::Critic::Policy';
  1         3  
  1         7  
8              
9             our $VERSION = 'v1.0.1';
10              
11 1     1   102 use constant DESC => 'Using bareword filehandles';
  1         4  
  1         110  
12 1     1   8 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         341  
13              
14 6     6 0 106138 sub supported_parameters { () }
15 18     18 1 312 sub default_severity { $SEVERITY_HIGH }
16 0     0 1 0 sub default_themes { 'community' }
17 6     6 1 269827 sub applies_to { 'PPI::Token::Word' }
18              
19             my %openers = (
20             accept => 1,
21             open => 1,
22             opendir => 1,
23             pipe => 2,
24             socket => 1,
25             socketpair => 2,
26             );
27              
28             my %builtins = (
29             ARGV => 1,
30             ARGVOUT => 1,
31             DATA => 1,
32             STDERR => 1,
33             STDIN => 1,
34             STDOUT => 1,
35             );
36              
37             sub violates {
38 229     229 1 2111 my ($self, $elem) = @_;
39 229 100 66     521 return () unless exists $openers{$elem} and is_function_call $elem;
40 90         23452 my $num_handles = $openers{$elem};
41            
42 90         525 my @args = parse_arg_list $elem;
43 90         18184 my @handles = splice @args, 0, $num_handles;
44            
45 90         177 my @violations;
46 90         170 foreach my $handle (@handles) {
47 97   50     922 my $name = pop @$handle // next;
48             push @violations, $self->violation(DESC, EXPL, $elem)
49 97 100 100     447 if $name->isa('PPI::Token::Word') and !exists $builtins{$name};
50             }
51            
52 90         3510 return @violations;
53             }
54              
55             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>