« blib/lib/Perl/Critic/Policy/BuiltinFunctions/RequireBlockGrep.pm
↑ Summary
blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitInterpolationOfLiterals.pm »
| 1 | package Perl::Critic::Policy::InputOutput::ProhibitJoinedReadline; | ||||||||||
| 2 | |||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 40 | ▶ | use List::SomeUtils qw(any); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities :classification parse_arg_list }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 40 | our $VERSION = '1.150'; | |||||||||
| 13 | |||||||||||
| 14 | #----------------------------------------------------------------------------- | ||||||||||
| 15 | |||||||||||
| 16 | 40 | Readonly::Scalar my $DESC => q{Use "local $/ = undef" or Path::Tiny instead of joined readline}; ## no critic qw(InterpolationOfMetachars) | |||||||||
| 17 | 40 | Readonly::Scalar my $EXPL => [213]; | |||||||||
| 18 | |||||||||||
| 19 | #----------------------------------------------------------------------------- | ||||||||||
| 20 | |||||||||||
| 21 | 90 | ▶ | sub supported_parameters { return () } | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 22 | 75 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 23 | 86 | ▶ | sub default_themes { return qw( core pbp performance ) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | 31 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | |||||||||||
| 26 | #----------------------------------------------------------------------------- | ||||||||||
| 27 | |||||||||||
| 28 | sub violates { | ||||||||||
| 29 | 332 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||
|
Subroutine
violates: called CC=4 SCAR=25.6
Pod
documented
| |||||||||||
| 30 | |||||||||||
| 31 | 332 | ▶ | return if $elem->content() ne 'join'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | ▶ | return if ! is_function_call($elem); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | my @args = parse_arg_list($elem); | |||||||||
| 34 | 0 | shift @args; # ignore separator string | |||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | if (any { any { $_->isa('PPI::Token::QuoteLike::Readline') } @{$_} } @args) { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 37 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 0 | return; # OK | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | 40 | 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 : | ||||||||||