1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/InputOutput/ProhibitReadlineInForLoop.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::InputOutput::ProhibitReadlineInForLoop;
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Readline inside "for" loop};
23 Readonly::Scalar my $EXPL => [ 211 ];
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_HIGH }
29 sub default_themes { return qw( core bugs pbp ) }
30 sub applies_to { return qw( PPI::Structure::ForLoop) }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
37 if ( my $rl = $elem->find_first('PPI::Token::QuoteLike::Readline') ) {
38 return $self->violation( $DESC, $EXPL, $rl );
48 #-----------------------------------------------------------------------------
54 Perl::Critic::Policy::InputOutput::ProhibitReadlineInForLoop - Write C<< while( $line = <> ){...} >> instead of C<< for(<>){...} >>.
58 This Policy is part of the core L<Perl::Critic> distribution.
63 Using the readline operator in a C<for> or C<foreach> loop is very
64 slow. The iteration list of the loop creates a list context,
65 which causes the readline operator to read the entire input stream
66 before iteration even starts. Instead, just use a C<while> loop,
67 which only reads one line at a time.
69 for my $line ( <$file_handle> ){ do_something($line) } #not ok
70 while ( my $line = <$file_handle> ){ do_something($line) } #ok
75 This Policy is not configurable except for the standard options.
80 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
84 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
86 This program is free software; you can redistribute it and/or modify
87 it under the same terms as Perl itself. The full text of this license
88 can be found in the LICENSE file included with this module.
94 # cperl-indent-level: 4
96 # indent-tabs-mode: nil
97 # c-indentation-style: bsd
99 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :