Search code examples
perlif-statementconditional-statementsinverse

Negating a regex pattern match


Is there a way to negate the elsif statement with the following pattern match statement, something like $ip != $testip?

I know that != is only for integers, I just wanted to explain what I want: the same function as != but for strings when pattern matching.

if ( $testip =~ /$ip/ ) {
    $frequency++;
}
elsif ( $ip ne $testip ) {

Solution

  • To negate matching, you can use

    $testip !~ /$ip/
    

    but there's no need to know, you can just negate the operator

    not $testip =~ /$ip/
    

    But why? The else branch is only executed when the if condition returns false.