Commit | Line | Data |
---|---|---|
34167feb JRT |
1 | ################################################################## |
2 | # $URL$ | |
3 | # $Date$ | |
4 | # $Author$ | |
5 | # $Revision$ | |
6 | ################################################################## | |
7 | ||
59b05e08 JRT |
8 | package Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval; |
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 'eval'}; | |
20 | my $expl = [161]; | |
21 | ||
22 | #---------------------------------------------------------------------------- | |
23 | ||
24 | sub violates { | |
25 | my ( $self, $elem, $doc ) = @_; | |
26 | $elem->isa('PPI::Token::Word') && $elem eq 'eval' || return; | |
27 | return if is_hash_key($elem); | |
28 | ||
29 | my $sib = $elem->snext_sibling() || return; | |
30 | my $arg = $sib->isa('PPI::Structure::List') ? $sib->schild(0) : $sib; | |
31 | return if !$arg || $arg->isa('PPI::Structure::Block'); | |
32 | ||
33 | #Must not be a block | |
34 | return Perl::Critic::Violation->new( $desc, $expl, $elem->location() ); | |
35 | } | |
36 | ||
37 | ||
38 | 1; | |
39 | ||
40 | __END__ | |
41 | ||
42 | =head1 NAME | |
43 | ||
44 | Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval | |
45 | ||
46 | =head1 DESCRIPTION | |
47 | ||
48 | The string form of eval is recompiled every time it is executed, | |
49 | whereas the block form is only compiled once. Also, the string form | |
50 | doesn't give compile-time warnings. | |
51 | ||
52 | eval "print $foo"; #not ok | |
53 | eval {print $foo}; #ok | |
54 | ||
55 | =head1 SEE ALSO | |
56 | ||
57 | L<Perl::Critic::Policy::ControlStrucutres::ProhibitStringyGrep> | |
58 | ||
59 | L<Perl::Critic::Policy::ControlStrucutres::ProhibitStringyMap> | |
60 | ||
61 | =head1 AUTHOR | |
62 | ||
63 | Jeffrey Ryan Thalhammer <thaljef@cpan.org> | |
64 | ||
65 | Copyright (c) 2005 Jeffrey Ryan Thalhammer. All rights reserved. | |
66 | ||
67 | This program is free software; you can redistribute it and/or modify | |
68 | it under the same terms as Perl itself. The full text of this license | |
69 | can be found in the LICENSE file included with this module. |