WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Basic · question 10 of 100

Explain the differences between ’==’, ’eq’, ’!=’, and ’ne’ operators in Perl.?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

In Perl, we use different comparison operators depending on the type of values we are comparing. The four most common comparison operators are ’==’, ’eq’, ’!=’, and ’ne’. Lets discuss their differences:

1. ’==’ Operator: The == operator is used to test whether two values are numerically equal or not. This operator does not consider the data types of the operands being compared. For example:

my $a = 5;
my $b = "5";
if ($a == $b) {
  print "Equal";
} else {
  print "Not Equal";
}

In this example, output will be "Equal" because even though $a is an integer and $b is a string, their numerical values are the same.

2. ’eq’ Operator: The ’eq’ operator is used to compare two values for string equality. Unlike the ’==’ operator, this operator pays attention to the data types of the operands being compared. For example:

my $x = "Hello";
my $y = "hello";
if ($x eq $y) {
  print "Equal";
} else {
  print "Not Equal";
}

In this example, output will be "Not Equal" because the strings are not identical.

3. ’!=’ Operator: The ’!=’ operator is used to test whether two values are not equal or not. This operator is used for numeric as well as string comparisons. For example:

my $m = 10;
my $n = 20;
if ($m != $n) {
  print "Not Equal";
} else {
  print "Equal";
}

In this example, output will be "Not Equal" because $m and $n are different.

4. ’ne’ Operator: The ’ne’ operator is used to test whether two values are not equal or not for strings. For numeric comparisons, ’!=’ operator is used. For example:

my $p = "Hello";
my $q = "hello";
if ($p ne $q) {
  print "Not Equal";
} else {
  print "Equal";
}

In this example, output will be "Not Equal" because $p and $q are different strings.

To sum up, while ’==’ and ’eq’ operators are used to test for numerical equality and string equality, respectively; ’!=’ and ’ne’ operators are used for inequality comparisons both numerically and by string value.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics