Anthony 'Runt' Cleaves Anthony 'Runt' Cleaves - 10 months ago 44
Perl Question

Perl regex hitting condition when not matching

I am checking a status of a program and acting apon it.

So I either receive all, local or an error.

However upon checking it with my

I am always hitting the condition even when it doesn't match.

if ($pri !~ /local/ || $pri !~ /all/) {
print $pri."\n";
print "[PRIMARY] Heartbeat is not responding correctly \n";

Above print displays as:

perl -p xx.xx.xx.xx -s xx.xx.xx.xx
[PRIMARY] Heartbeat is not responding correctly

And a dump looks like

perl -p xx.xx.xx.xx -s xx.xx.xx.xx
$VAR1 = 'all';
[PRIMARY] Heartbeat is not responding correctly

Am I missing something obvious? To me the condition should be skipped as the value is all.

Any feedback would be appreciated.


The regexp is almost fine, you just used wrong logical operator - or. It should be and:

if ($pri !~ /local/ && $pri !~ /all/) {

Also for regexp consider surrounding by ^ and $ to have full string match.