File Coverage

blib/lib/Perl/Critic/Policy/InputOutput/ProhibitExplicitStdin.pm
Criterion Covered Total %
statement 24 24 100.0
branch 2 2 100.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 41 42 97.6


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::InputOutput::ProhibitExplicitStdin;
2              
3 40     40   27219 use 5.010001;
  40         184  
4 40     40   288 use strict;
  40         107  
  40         867  
5 40     40   219 use warnings;
  40         135  
  40         1194  
6 40     40   250 use Readonly;
  40         105  
  40         2497  
7              
8 40     40   334 use Perl::Critic::Utils qw{ :severities :classification &parse_arg_list };
  40         150  
  40         2256  
9 40     40   15045 use parent 'Perl::Critic::Policy';
  40         129  
  40         268  
10              
11             our $VERSION = '1.146';
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 92     92 0 1671 sub supported_parameters { return () }
21 81     81 1 411 sub default_severity { return $SEVERITY_HIGH }
22 86     86 1 396 sub default_themes { return qw( core pbp maintenance ) }
23 35     35 1 126 sub applies_to { return 'PPI::Token::QuoteLike::Readline' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 10     10 1 27 my ( $self, $elem, undef ) = @_;
29              
30 10 100       33 return if $elem ne '<STDIN>';
31 7         145 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-2011 Chris Dolan. Many rights reserved.
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 :