File Coverage

blib/lib/Perl/Critic/Policy/Freenode/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::Freenode::BarewordFilehandles;
2              
3 1     1   691 use strict;
  1         2  
  1         31  
4 1     1   6 use warnings;
  1         2  
  1         30  
5              
6 1     1   6 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         2  
  1         54  
7 1     1   403 use parent 'Perl::Critic::Policy';
  1         2  
  1         14  
8              
9             our $VERSION = '0.033';
10              
11 1     1   92 use constant DESC => 'Using bareword filehandles';
  1         3  
  1         70  
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         309  
13              
14 6     6 0 89634 sub supported_parameters { () }
15 18     18 1 282 sub default_severity { $SEVERITY_HIGH }
16 0     0 1 0 sub default_themes { 'freenode' }
17 6     6 1 247801 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 1917 my ($self, $elem) = @_;
39 229 100 66     494 return () unless exists $openers{$elem} and is_function_call $elem;
40 90         22442 my $num_handles = $openers{$elem};
41            
42 90         498 my @args = parse_arg_list $elem;
43 90         17188 my @handles = splice @args, 0, $num_handles;
44            
45 90         159 my @violations;
46 90         167 foreach my $handle (@handles) {
47 97   50     864 my $name = pop @$handle // next;
48             push @violations, $self->violation(DESC, EXPL, $elem)
49 97 100 100     393 if $name->isa('PPI::Token::Word') and !exists $builtins{$name};
50             }
51            
52 90         3281 return @violations;
53             }
54              
55             1;
56              
57             =head1 NAME
58              
59             Perl::Critic::Policy::Freenode::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::Freenode>.
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>