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   23915 use 5.010001;
  40         144  
4 40     40   191 use strict;
  40         78  
  40         845  
5 40     40   192 use warnings;
  40         97  
  40         1576  
6 40     40   169 use Readonly;
  40         83  
  40         2185  
7 40     40   194 use List::SomeUtils qw(any);
  40         74  
  40         1918  
8              
9 40     40   187 use Perl::Critic::Utils qw{ :severities :classification parse_arg_list };
  40         81  
  40         2011  
10 40     40   11652 use parent 'Perl::Critic::Policy';
  40         83  
  40         221  
11              
12             our $VERSION = '1.156';
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 90     90 0 581 sub supported_parameters { return () }
22 75     75 1 234 sub default_severity { return $SEVERITY_MEDIUM }
23 86     86 1 240 sub default_themes { return qw( core pbp performance ) }
24 31     31 1 82 sub applies_to { return 'PPI::Token::Word' }
25              
26             #-----------------------------------------------------------------------------
27              
28             sub violates {
29 332     332 1 468 my ( $self, $elem, undef ) = @_;
30              
31 332 50       518 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 :