File Coverage

blib/lib/Perl/Critic/Policy/InputOutput/ProhibitJoinedReadline.pm
Criterion Covered Total %
statement 34 35 97.1
branch 5 6 83.3
condition n/a
subroutine 13 13 100.0
pod 4 5 80.0
total 56 59 94.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::InputOutput::ProhibitJoinedReadline;
2              
3 40     40   28086 use 5.010001;
  40         191  
4 40     40   254 use strict;
  40         118  
  40         914  
5 40     40   213 use warnings;
  40         110  
  40         1054  
6 40     40   235 use Readonly;
  40         97  
  40         2388  
7 40     40   288 use List::SomeUtils qw(any);
  40         117  
  40         2081  
8              
9 40     40   323 use Perl::Critic::Utils qw{ :severities :classification parse_arg_list };
  40         145  
  40         2227  
10 40     40   17610 use parent 'Perl::Critic::Policy';
  40         133  
  40         272  
11              
12             our $VERSION = '1.146';
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 93     93 0 1686 sub supported_parameters { return () }
22 86     86 1 412 sub default_severity { return $SEVERITY_MEDIUM }
23 86     86 1 363 sub default_themes { return qw( core pbp performance ) }
24 34     34 1 110 sub applies_to { return 'PPI::Token::Word' }
25              
26             #-----------------------------------------------------------------------------
27              
28             sub violates {
29 351     351 1 667 my ( $self, $elem, undef ) = @_;
30              
31 351 100       690 return if $elem->content() ne 'join';
32 13 100       89 return if ! is_function_call($elem);
33 12         38 my @args = parse_arg_list($elem);
34 12         25 shift @args; # ignore separator string
35              
36 12 50   12   66 if (any { any { $_->isa('PPI::Token::QuoteLike::Readline') } @{$_} } @args) {
  12         34  
  12         51  
  12         30  
37 12         47 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 :