« blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitSleepViaSelect.pm
↑ Summary
blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitStringySplit.pm »
| 1 | package Perl::Lint::Policy::BuiltinFunctions::ProhibitStringyEval; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Perl::Lint::Constants::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use parent "Perl::Lint::Policy"; | ||
| 6 | |||
| 7 | use constant { | ||
| 8 | DESC => 'Expression form of "eval"', | ||
| 9 | EXPL => [161], | ||
| 10 | }; | ||
| 11 | |||
| 12 | sub evaluate { | ||
| 13 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 14 | |||
| 15 | my $allow_includes = $args->{prohibit_stringy_eval}->{allow_includes} || 0; | ||
| 16 | |||
| 17 | my @violations; | ||
| 18 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 19 | my $token_type = $token->{type}; | ||
| 20 | my $token_data = $token->{data}; | ||
| 21 | |||
| 22 | if ($token_type == BUILTIN_FUNC && $token_data eq 'eval') { | ||
| 23 | $token = $tokens->[++$i]; | ||
| 24 | if ($token->{type} == LEFT_PAREN) { | ||
| 25 | $token = $tokens->[++$i]; | ||
| 26 | if ($token->{type} == RIGHT_PAREN) { | ||
| 27 | next; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | $token_type = $token->{type}; | ||
| 31 | |||
| 32 | if ($token_type != LEFT_BRACE) { | ||
| 33 | if ($allow_includes) { | ||
| 34 | if ($token_type == STRING) { | ||
| 35 | if ($token->{data} =~ /\A(?:use|require)[^;]*(?:;|;\s*1;)?\Z/) { | ||
| 36 | next; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | elsif ($token_type == RAW_STRING) { | ||
| 40 | if ($token->{data} =~ /\A(?:use|require)\s+([^;\s]+)[^;]*(?:;|;\s*1;)?\Z/) { | ||
| 41 | if ($1 !~ /\A\$/) { | ||
| 42 | next; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | elsif ($token_type == REG_DOUBLE_QUOTE) { | ||
| 47 | $i += 2; # skip reg delimiter | ||
| 48 | $token = $tokens->[$i]; | ||
| 49 | if ($token->{data} =~ /\A(?:use|require)[^;]*(?:;|;\s*1;)?\Z/) { | ||
| 50 | next; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | elsif ($token_type == REG_QUOTE) { | ||
| 54 | $i += 2; # skip reg delimiter | ||
| 55 | $token = $tokens->[$i]; | ||
| 56 | if ($token->{data} =~ /\A(?:use|require)\s+([^;\s]+)[^;]*(?:;|;\s*1;)?\Z/) { | ||
| 57 | if ($1 !~ /\A\$/) { | ||
| 58 | next; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | push @violations, { | ||
| 65 | filename => $file, | ||
| 66 | line => $token->{line}, | ||
| 67 | description => DESC, | ||
| 68 | explanation => EXPL, | ||
| 69 | policy => __PACKAGE__, | ||
| 70 | }; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | return \@violations; | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; | ||
| 79 | |||