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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download