1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitVoidGrep.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::BuiltinFunctions::ProhibitVoidGrep;
15 use Perl::Critic::Utils qw{ :severities :classification is_in_void_context };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{"grep" used in void context};
23 Readonly::Scalar my $EXPL => q{Use a "for" loop instead};
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_MEDIUM }
29 sub default_themes { return qw( core maintenance ) }
30 sub applies_to { return 'PPI::Token::Word' }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
37 return if $elem ne 'grep';
38 return if not is_function_call($elem);
39 return if not is_in_void_context($elem);
41 return $self->violation( $DESC, $EXPL, $elem );
49 #-----------------------------------------------------------------------------
55 Perl::Critic::Policy::BuiltinFunctions::ProhibitVoidGrep - Don't use C<grep> in void contexts.
59 This Policy is part of the core L<Perl::Critic> distribution.
64 C<map> and C<grep> are intended to be pure functions, not mutators.
65 If you want to iterate with side-effects, then you should use a proper
66 C<for> or C<foreach> loop.
68 grep{ print frobulate($_) } @list; #not ok
69 print map{ frobulate($_) } @list; #ok
71 grep{ $_ = lc $_ } @list; #not ok
72 for( @list ){ $_ = lc $_ }; #ok
74 map{ push @frobbed, frobulate($_) } @list; #not ok
75 @frobbed = map { frobulate($_) } @list; #ok
80 This Policy is not configurable except for the standard options.
85 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
89 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
91 This program is free software; you can redistribute it and/or modify
92 it under the same terms as Perl itself. The full text of this license
93 can be found in the LICENSE file included with this module.
99 # cperl-indent-level: 4
101 # indent-tabs-mode: nil
102 # c-indentation-style: bsd
104 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :