Commit | Line | Data |
---|---|---|
34167feb JRT |
1 | ################################################################## |
2 | # $URL$ | |
3 | # $Date$ | |
4 | # $Author$ | |
5 | # $Revision$ | |
6 | ################################################################## | |
7 | ||
59b05e08 JRT |
8 | package Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap; |
9 | ||
10 | use strict; | |
11 | use warnings; | |
12 | use Perl::Critic::Utils; | |
13 | use Perl::Critic::Violation; | |
14 | use base 'Perl::Critic::Policy'; | |
15 | ||
16 | our $VERSION = '0.13'; | |
17 | $VERSION = eval $VERSION; ## no critic | |
18 | ||
19 | my $desc = q{Expression form of 'map'}; | |
20 | my $expl = [169]; | |
21 | ||
22 | #---------------------------------------------------------------------------- | |
23 | ||
24 | sub violates { | |
25 | my ( $self, $elem, $doc ) = @_; | |
26 | $elem->isa('PPI::Token::Word') && $elem eq 'map' || 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 | ||
39 | 1; | |
40 | ||
41 | __END__ | |
42 | ||
43 | =head1 NAME | |
44 | ||
45 | Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap | |
46 | ||
47 | =head1 DESCRIPTION | |
48 | ||
49 | The expression form of C<grep> and C<map> is awkward and hard to read. | |
50 | Use 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 | ||
61 | L<Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval> | |
62 | ||
63 | L<Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep> | |
64 | ||
65 | =head1 AUTHOR | |
66 | ||
67 | Jeffrey Ryan Thalhammer <thaljef@cpan.org> | |
68 | ||
69 | Copyright (c) 2005 Jeffrey Ryan Thalhammer. All rights reserved. | |
70 | ||
71 | This program is free software; you can redistribute it and/or modify | |
72 | it under the same terms as Perl itself. The full text of this license | |
73 | can be found in the LICENSE file included with this module. |