« blib/lib/Perl/Lint/Policy/InputOutput/RequireCheckedOpen.pm
↑ Summary
blib/lib/Perl/Lint/Policy/Miscellanea/ProhibitFormats.pm »
| 1 | package Perl::Lint::Policy::InputOutput::RequireEncodingWithUTF8Layer; | ||
| 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::Kind; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use Perl::Lint::Constants::Type; | ||
| 6 | use parent "Perl::Lint::Policy"; | ||
| 7 | |||
| 8 | use constant { | ||
| 9 | DESC => 'I/O layer ":utf8" used', | ||
| 10 | EXPL => 'Use ":encoding(UTF-8)" to get strict validation', | ||
| 11 | }; | ||
| 12 | |||
| 13 | sub evaluate { | ||
| 14 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 15 | |||
| 16 | my @violations; | ||
| 17 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 18 | my $token_type = $token->{type}; | ||
| 19 | my $token_data = $token->{data}; | ||
| 20 | |||
| 21 | if ($token_type == BUILTIN_FUNC && ($token_data eq 'open' || $token_data eq 'binmode')) { | ||
| 22 | my @args; | ||
| 23 | for ($i++; my $token = $tokens->[$i]; $i++) { | ||
| 24 | my $token_type = $token->{type}; | ||
| 25 | my $token_kind = $token->{kind}; | ||
| 26 | if ($token_type == RIGHT_PAREN || $token_kind == KIND_STMT_END || $token_kind == KIND_OP) { | ||
| 27 | last; | ||
| 28 | } | ||
| 29 | elsif ( | ||
| 30 | $token_type != COMMA && | ||
| 31 | $token_type != REG_DOUBLE_QUOTE && | ||
| 32 | $token_type != REG_QUOTE && | ||
| 33 | $token_type != REG_DELIM && | ||
| 34 | $token_type != LEFT_PAREN && | ||
| 35 | $token_kind != KIND_DECL | ||
| 36 | ) { | ||
| 37 | push @args, $token->{data}; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | my $second_arg = $args[1]; | ||
| 41 | if ($second_arg && $second_arg =~ /utf8\Z/) { | ||
| 42 | push @violations, { | ||
| 43 | filename => $file, | ||
| 44 | line => $token->{line}, | ||
| 45 | description => DESC, | ||
| 46 | explanation => EXPL, | ||
| 47 | policy => __PACKAGE__, | ||
| 48 | }; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | return \@violations; | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; | ||
| 58 | |||