Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Useless use of negative pattern binding (!~) in void context

Tags:

perl

If both strings have spaces or neither has spaces, then do something.

my $with_spaces = $a =~ / / and $b =~ / /;
my $no_spaces = $a !~ / / and $b !~ / /;
if ($with_spaces or $no_spaces) {
    dosomething();
}

But this code gives an error:

Useless use of negative pattern binding (!~) in void context.

Did I do something wrong here?

like image 908
jonah_w Avatar asked Mar 04 '23 04:03

jonah_w


1 Answers

The lines:

my $with_spaces = $a =~ / / and $b =~ / /;
my $no_spaces = $a !~ / / and $b !~ / /;

are equivalent to:

(my $with_spaces = $a =~ / /) and ($b =~ / /);
(my $no_spaces = $a !~ / /) and ($b !~ / /);

Either use && instead of and, or add parentheses to change the precedence:

my $with_spaces = $a =~ / / && $b =~ / /;
my $no_spaces = ($a !~ / / and $b !~ / /);
like image 124
jhnc Avatar answered Mar 12 '23 17:03

jhnc