Perlcritic: eval "requires $ module";

While digging in some old source code, I saw the following:

my $module = $some{module};
eval "require $module";
die "Bad module\n$@" if $@;

as long as I understand what the code does, it tries to β€œrequire” the module and die when it is unsuccessful - perlcritic complains about it

The form of the expression "eval" is on line 331, column 13. See page 161 of PBP. (Severity level: 5)

Unfortunately, I don't have a PBP book, so I wonder what is the correct method above ...

Also in the same source found:

sub test_repo_file {
    my($self, $repo, $test) = @_;
    my $abspath = repo_abs_path($repo);
    return "eval -$test $abspath";
}

It is not clear what "eval" solves, and perlcritic complains again about "string eval" ...

Can someone explain the main points about "string eval" and how to write above correctly?

+4
source share
2

perlcritic --verbose '%d\n' :

`eval ' , ,      . ,      .

   eval "print $foo";        # not ok
   eval {print $foo};        # ok

.

.

return eval "-$test $abspath"

eval. , $test ,

$test =~ /^[a-z]$/i

$abspath:

eval "-$test \$abspath"

,

## no critic

.

+7

- eval EXPR. , (, s/.../eval($repl)/e), , eval BLOCK .

, . , . . , eval EXPR .

perlcritic , , .


eval EXPR .

my $path = $module . ".pm";
$path =~ s{::}{/}g;
eval { require $path }

, .

+3

All Articles