vendredi 26 juin 2020

String matching in Perl for multiple patterns

I have a string, and when I check it against with the variable its not giving me expected result. By using pattern matching mechanism it works fine.

Is this expected behavior in Perl. If not, how to resolve this without using pattern matching.

Code Below:

#!/usr/bin/perl

use strict; 
use warnings;

my $day_of_week = "Monday";

if (($day_of_week ne "Monday") or ($day_of_week ne "Tuesday")){
    print "In If. Today is $day_of_week.\n";
} else {
    print "In else part\n";
}

This should give output as:

In else part

But its giving me:

In If. Today is Monday.

When I change the if condtion with pattern matching like below, it works fine.

if ($day_of_week !~ /Monday|Tuesday) {

My concern is why the string matching is not working as per the condition. Am I doing something wrong here.

Aucun commentaire:

Enregistrer un commentaire