Login
Added svn keywords.
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / BuiltinFunctions / RequireBlockGrep.pm
CommitLineData
34167feb
JRT
1##################################################################
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6##################################################################
7
59b05e08
JRT
8package Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep;
9
10use strict;
11use warnings;
12use Perl::Critic::Utils;
13use Perl::Critic::Violation;
14use base 'Perl::Critic::Policy';
15
16our $VERSION = '0.13';
17$VERSION = eval $VERSION; ## no critic
18
19my $desc = q{Expression form of 'grep'};
20my $expl = [169];
21
22#----------------------------------------------------------------------------
23
24sub violates {
25 my ( $self, $elem, $doc ) = @_;
26 $elem->isa('PPI::Token::Word') && $elem eq 'grep' || return;
27 return if is_method_call($elem);
28 return if is_hash_key($elem);
29
30 my $sib = $elem->snext_sibling() || return;
31 my $arg = $sib->isa('PPI::Structure::List') ? $sib->schild(0) : $sib;
32 return if !$arg || $arg->isa('PPI::Structure::Block');
33
34 #Must not be a block
35 return Perl::Critic::Violation->new( $desc, $expl, $elem->location() );
36}
37
38
391;
40
41__END__
42
43=head1 NAME
44
45Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep
46
47=head1 DESCRIPTION
48
49The expression form of C<grep> and C<map> is awkward and hard to read.
50Use the block forms instead.
51
52 @matches = grep /pattern/, @list; #not ok
53 @matches = grep { /pattern/ } @list; #ok
54
55 @mapped = map transform($_), @list; #not ok
56 @mapped = map { transform($_) } @list; #ok
57
58
59=head1 SEE ALSO
60
61L<Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval>
62
63L<Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap>
64
65=head1 AUTHOR
66
67Jeffrey Ryan Thalhammer <thaljef@cpan.org>
68
69Copyright (c) 2005 Jeffrey Ryan Thalhammer. All rights reserved.
70
71This program is free software; you can redistribute it and/or modify
72it under the same terms as Perl itself. The full text of this license
73can be found in the LICENSE file included with this module.