ecm has asked for the wisdom of the Perl Monks concerning the following question:
I had the following condition in a script that filtered out certain lines:
if (not $inst =~ /lea/i) { # do things }
This worked as intended, the condition being wrong if the $inst string variable contains "lea" as a substring. Now I had another variable which I would also like to check to do the same filtering. This is what I tried: (Note that I assume the \b are irrelevant.)
if (not $label.$inst =~ /\blea\b/i) { # do things }
A few hours later I found that this condition is never true, assuming $label and $inst both being nonempty strings and both not strings of a single zero "0". I eventually fixed this like so:
if (not (($label.$inst) =~ /\blea\b/i)) { # do things }
This works as expected: The concatenation happens first, then it is matched against the pattern, and then the pattern result is negated in a boolean sense.
However, I'm wondering why both additional sets of parens are needed here. According to perlop the match operator "=~" has a higher precedence than the string concatenation ".", so I understand why I need the innermost parens - to have perl concatenate first before applying the match pattern. But the same page states that the "not" operator has a lower precedence than both of the others. So I would expect the following to work the way I want, but it doesn't seem to:
if (not ($label.$inst) =~ /\blea\b/i) { # do things }
So what exactly is going on there? Is the "not" somehow applied to "($label.$inst)" before the match operator is processed?
In my repo this has additional conditions in the same "if" condition but some light testing seems to indicate this isn't the cause of the problem. Here's two simple stand alone test scriptlets:
$ perl -e 'use warnings; use strict; my $pre="foo"; my $suf="bar"; pri +nt (not (($pre.$suf) =~ /lea/i)); print "\n";' 1 $ perl -e 'use warnings; use strict; my $pre="foo"; my $suf="bar"; pri +nt (not ($pre.$suf) =~ /lea/i); print "\n";' $
I expected both scriptlets to display a "1" (true condition output).
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Why does NOT operator on match operator on string concatenation require two pairs of parens?
by GrandFather (Saint) on Nov 25, 2024 at 00:28 UTC | |
Re: Why does NOT operator on match operator on string concatenation require two pairs of parens?
by tybalt89 (Monsignor) on Nov 24, 2024 at 23:00 UTC | |
Re: Why does NOT operator on match operator on string concatenation require two pairs of parens?
by jwkrahn (Abbot) on Nov 24, 2024 at 22:09 UTC | |
Re: Why does NOT operator on match operator on string concatenation require two pairs of parens?
by LanX (Saint) on Nov 24, 2024 at 23:18 UTC |