GMichael
GMichael

Reputation: 2776

Perl compilation problem that Perl::Critic does not see

I have this if statement that does not pass compilation:

        my $string_var;
        if ( $string_var eq "string_value1" || $string_var eq "string_value2" ) &&
           (defined $ENV{VAR_NAME}) {
                die { "error_message" => "error_message" }
        }

Neither I nor Perl::Critic see a problem. But Perl says:

syntax error at /opt/app_protect/bin/../lib/perl/F5/BdCfg/Bundle.pm line 178, near ") &&"
syntax error at /opt/app_protect/bin/../lib/perl/F5/BdCfg/Bundle.pm line 181, near "}"

Can anyone help?

It's CentOS 7.6.1810 with Perl v5.16.3

Upvotes: 0

Views: 75

Answers (2)

U. Windl
U. Windl

Reputation: 4325

Perl::Criticis not intended to handle syntax errors; instead it will consider semantic issues, I guess.

Whether Perl::Critic could or should handle syntax errors is a different question, however.

When writing Perl code, it might make sense to try validating the code using perl -c your_program before trying to run or improve it (as Perl::Critic might suggest). Sometimes perl -c gives better diagnostics than trying to run the code.

Upvotes: 0

Steffen Ullrich
Steffen Ullrich

Reputation: 123375

From perlsyn:

The following compound statements may be used to control flow:
...
if (EXPR) BLOCK

What you provide here is not

if (EXPR) BLOCK

but instead

if (EXPR1) && (EXPR2) BLOCK

This needs to be enclosed in parentheses in order to be a valid syntax, i.e.

if ((EXPR1) && (EXPR2)) BLOCK

Upvotes: 7

Related Questions