File Coverage

blib/lib/Perl/Critic/Policy/Community/POSIXImports.pm
Criterion Covered Total %
statement 24 25 96.0
branch 2 2 100.0
condition 5 10 50.0
subroutine 10 11 90.9
pod 4 5 80.0
total 45 53 84.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Community::POSIXImports;
2              
3 1     1   509 use strict;
  1         2  
  1         31  
4 1     1   5 use warnings;
  1         3  
  1         29  
5              
6 1     1   7 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         2  
  1         104  
7 1     1   460 use parent 'Perl::Critic::Policy';
  1         3  
  1         7  
8              
9             our $VERSION = 'v1.0.1';
10              
11 1     1   109 use constant DESC => 'Using POSIX.pm without an explicit import list';
  1         2  
  1         86  
12 1     1   8 use constant EXPL => 'Using the POSIX module without specifying an import list results in importing hundreds of symbols. Import the functions or constants you want explicitly, or prevent the import with ().';
  1         2  
  1         217  
13              
14 4     4 0 15313 sub supported_parameters { () }
15 2     2 1 219 sub default_severity { $SEVERITY_LOW }
16 0     0 1 0 sub default_themes { 'community' }
17 4     4 1 24390 sub applies_to { 'PPI::Statement::Include' }
18              
19             sub violates {
20 5     5 1 99 my ($self, $elem) = @_;
21 5 100 50     19 return $self->violation(DESC, EXPL, $elem) if ($elem->type // '') eq 'use'
      50        
      33        
      66        
22             and ($elem->module // '') eq 'POSIX' and !$elem->arguments;
23 3         263 return ();
24             }
25              
26             1;
27              
28             =head1 NAME
29              
30             Perl::Critic::Policy::Community::POSIXImports - Don't use POSIX without
31             specifying an import list
32              
33             =head1 DESCRIPTION
34              
35             The L<POSIX> module imports hundreds of symbols (functions and constants) by
36             default for backwards compatibility reasons. To avoid this, and to assist in
37             finding where functions have been imported from, specify the symbols you want
38             to import explicitly in the C<use> statement. Alternatively, specify an empty
39             import list with C<use POSIX ()> to avoid importing any symbols, and fully
40             qualify the functions or constants, such as C<POSIX::strftime>.
41              
42             use POSIX; # not ok
43             use POSIX (); # ok
44             use POSIX 'fcntl'; # ok
45             use POSIX qw(O_APPEND O_CREAT O_EXCL O_RDONLY O_RDWR O_WRONLY); # ok
46              
47             =head1 AFFILIATION
48              
49             This policy is part of L<Perl::Critic::Community>.
50              
51             =head1 CONFIGURATION
52              
53             This policy is not configurable except for the standard options.
54              
55             =head1 AUTHOR
56              
57             Dan Book, C<dbook@cpan.org>
58              
59             =head1 COPYRIGHT AND LICENSE
60              
61             Copyright 2015, Dan Book.
62              
63             This library is free software; you may redistribute it and/or modify it under
64             the terms of the Artistic License version 2.0.
65              
66             =head1 SEE ALSO
67              
68             L<Perl::Critic>