File Coverage

blib/lib/Perl/Critic/Policy/InputOutput/ProhibitJoinedReadline.pm
Criterion Covered Total %
statement 26 35 74.2
branch 1 6 16.6
condition n/a
subroutine 12 13 92.3
pod 4 5 80.0
total 43 59 72.8


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::InputOutput::ProhibitJoinedReadline;
2              
3 40     40   26810 use 5.010001;
  40         186  
4 40     40   271 use strict;
  40         121  
  40         865  
5 40     40   221 use warnings;
  40         95  
  40         1021  
6 40     40   246 use Readonly;
  40         130  
  40         2136  
7 40     40   291 use List::SomeUtils qw(any);
  40         99  
  40         2047  
8              
9 40     40   325 use Perl::Critic::Utils qw{ :severities :classification parse_arg_list };
  40         147  
  40         2299  
10 40     40   14150 use parent 'Perl::Critic::Policy';
  40         146  
  40         306  
11              
12             our $VERSION = '1.150';
13              
14             #-----------------------------------------------------------------------------
15              
16             Readonly::Scalar my $DESC => q{Use "local $/ = undef" or Path::Tiny instead of joined readline}; ## no critic qw(InterpolationOfMetachars)
17             Readonly::Scalar my $EXPL => [213];
18              
19             #-----------------------------------------------------------------------------
20              
21 89     89 0 1639 sub supported_parameters { return () }
22 74     74 1 296 sub default_severity { return $SEVERITY_MEDIUM }
23 86     86 1 356 sub default_themes { return qw( core pbp performance ) }
24 30     30 1 86 sub applies_to { return 'PPI::Token::Word' }
25              
26             #-----------------------------------------------------------------------------
27              
28             sub violates {
29 329     329 1 524 my ( $self, $elem, undef ) = @_;
30              
31 329 50       504 return if $elem->content() ne 'join';
32 0 0         return if ! is_function_call($elem);
33 0           my @args = parse_arg_list($elem);
34 0           shift @args; # ignore separator string
35              
36 0 0   0     if (any { any { $_->isa('PPI::Token::QuoteLike::Readline') } @{$_} } @args) {
  0            
  0            
  0            
37 0           return $self->violation( $DESC, $EXPL, $elem );
38             }
39              
40 0           return; # OK
41             }
42              
43              
44             1;
45              
46             __END__
47              
48             #-----------------------------------------------------------------------------
49              
50             =pod
51              
52             =head1 NAME
53              
54             Perl::Critic::Policy::InputOutput::ProhibitJoinedReadline - Use C<local $/ = undef> or L<Path::Tiny|Path::Tiny> instead of joined readline.
55              
56             =head1 AFFILIATION
57              
58             This Policy is part of the core L<Perl::Critic|Perl::Critic>
59             distribution.
60              
61              
62             =head1 DESCRIPTION
63              
64             It's really easy to slurp a whole filehandle in at once with C<join
65             q{}, <$fh>>, but that's inefficient -- Perl goes to the trouble of
66             splitting the file into lines only to have that work thrown away.
67              
68             To save performance, either slurp the filehandle without splitting
69             like so:
70              
71             do { local $/ = undef; <$fh> }
72              
73             or use L<Path::Tiny|Path::Tiny>, which is even faster.
74              
75             B<Note> that if the C<ProhibitPunctuationVars> policy is also in effect,
76             it will complain about the use of C<$/> in the line above. In that
77             case, write this instead:
78              
79             use English '-no_match_vars';
80              
81             do { local $INPUT_RECORD_SEPARATOR = undef; <$fh> };
82              
83              
84             =head1 CONFIGURATION
85              
86             This Policy is not configurable except for the standard options.
87              
88              
89             =head1 CAVEATS
90              
91             Due to a bug in the current version of PPI (v1.119_03) and earlier,
92             the readline operator is often misinterpreted as less-than and
93             greater-than operators after a comma. Therefore, this policy only
94             works well on the empty filehandle, C<< <> >>. When PPI is fixed,
95             this should just start working.
96              
97              
98             =head1 CREDITS
99              
100             Initial development of this policy was supported by a grant from the
101             Perl Foundation.
102              
103              
104             =head1 AUTHOR
105              
106             Chris Dolan <cdolan@cpan.org>
107              
108              
109             =head1 COPYRIGHT
110              
111             Copyright (c) 2007-2021 Chris Dolan. Many rights reserved.
112              
113             This program is free software; you can redistribute it and/or modify
114             it under the same terms as Perl itself. The full text of this license
115             can be found in the LICENSE file included with this module
116              
117             =cut
118              
119             # Local Variables:
120             # mode: cperl
121             # cperl-indent-level: 4
122             # fill-column: 78
123             # indent-tabs-mode: nil
124             # c-indentation-style: bsd
125             # End:
126             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :