File Coverage

blib/lib/Perl/Critic/Policy/InputOutput/ProhibitExplicitStdin.pm
Criterion Covered Total %
statement 21 24 87.5
branch 0 2 0.0
condition n/a
subroutine 10 11 90.9
pod 4 5 80.0
total 35 42 83.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::InputOutput::ProhibitExplicitStdin;
2              
3 40     40   26521 use 5.010001;
  40         168  
4 40     40   255 use strict;
  40         147  
  40         815  
5 40     40   244 use warnings;
  40         120  
  40         1030  
6 40     40   240 use Readonly;
  40         112  
  40         2148  
7              
8 40     40   285 use Perl::Critic::Utils qw( :severities :classification );
  40         103  
  40         1998  
9 40     40   14298 use parent 'Perl::Critic::Policy';
  40         123  
  40         259  
10              
11             our $VERSION = '1.150';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Use "<>" or "<ARGV>" or a prompting module instead of "<STDIN>"};
16             Readonly::Scalar my $EXPL => [216,220,221];
17              
18             #-----------------------------------------------------------------------------
19              
20 89     89 0 1593 sub supported_parameters { return () }
21 74     74 1 313 sub default_severity { return $SEVERITY_HIGH }
22 86     86 1 310 sub default_themes { return qw( core pbp maintenance ) }
23 32     32 1 83 sub applies_to { return 'PPI::Token::QuoteLike::Readline' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 0     0 1   my ( $self, $elem, undef ) = @_;
29              
30 0 0         return if $elem ne '<STDIN>';
31 0           return $self->violation( $DESC, $EXPL, $elem );
32             }
33              
34              
35             1;
36              
37             __END__
38              
39             #-----------------------------------------------------------------------------
40              
41             =pod
42              
43             =head1 NAME
44              
45             Perl::Critic::Policy::InputOutput::ProhibitExplicitStdin - Use "<>" or "<ARGV>" or a prompting module instead of "<STDIN>".
46              
47             =head1 AFFILIATION
48              
49             This Policy is part of the core L<Perl::Critic|Perl::Critic>
50             distribution.
51              
52              
53             =head1 DESCRIPTION
54              
55             Perl has a useful magic filehandle called C<*ARGV> that checks the
56             command line and if there are any arguments, opens and reads those as
57             files. If there are no arguments, C<*ARGV> behaves like C<*STDIN>
58             instead. This behavior is almost always what you want if you want to
59             create a program that reads from C<STDIN>. This is often written in
60             one of the following two equivalent forms:
61              
62             while (<ARGV>) {
63             # ... do something with each input line ...
64             }
65             # or, equivalently:
66             while (<>) {
67             # ... do something with each input line ...
68             }
69              
70             If you want to prompt for user input, try special purpose modules like
71             L<IO::Prompt|IO::Prompt>.
72              
73              
74             =head1 CONFIGURATION
75              
76             This Policy is not configurable except for the standard options.
77              
78              
79             =head1 CAVEATS
80              
81             Due to a bug in the current version of PPI (v1.119_03) and earlier,
82             the readline operator is often misinterpreted as less-than and
83             greater-than operators after a comma. Therefore, this policy misses
84             important cases like
85              
86             my $content = join '', <STDIN>;
87              
88             because it interprets that line as the nonsensical statement:
89              
90             my $content = join '', < STDIN >;
91              
92             When that PPI bug is fixed, this policy should start catching those
93             violations automatically.
94              
95             =head1 CREDITS
96              
97             Initial development of this policy was supported by a grant from the
98             Perl Foundation.
99              
100             =head1 AUTHOR
101              
102             Chris Dolan <cdolan@cpan.org>
103              
104             =head1 COPYRIGHT
105              
106             Copyright (c) 2007-2023 Chris Dolan
107              
108             This program is free software; you can redistribute it and/or modify
109             it under the same terms as Perl itself. The full text of this license
110             can be found in the LICENSE file included with this module
111              
112             =cut
113              
114             # Local Variables:
115             # mode: cperl
116             # cperl-indent-level: 4
117             # fill-column: 78
118             # indent-tabs-mode: nil
119             # c-indentation-style: bsd
120             # End:
121             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :