Home Perl Programming Page 5 - Perl 101 (Part 2) - Of Variables And Operators

# Comparing Apples And Oranges - Perl

Now that you've got the basics of the language down, this secondarticle in the series teaches you about Perl's variables and operators, andalso introduces you to conditional expressions.

Rating:  / 6
June 01, 2000

SEARCH DEV SHED

TOOLS YOU CAN USE
In addition to the various arithmetic and string operators, Perl also comes with a bunch of comparison operators, whose sole raison d'etre is to evaluate expressions and determine if they are true or false. Here's a list - you should use these operators for numeric comparisons only.

Assume \$x=4 and \$y=10

 Operator What It Means Expression Result == is equal to \$x == \$y False != is not equal to \$x != \$y True > is greater than \$x > \$y False < is less than \$x < \$y True >= is greater than or equal to \$x >= \$y False <= is less than or equal to \$x <= \$y True

If, however, you're planning to compare string values, the two most commonly used operators are the equality and inequality operators, as listed below.

Assume \$x="abc", \$y="xyz"

 Operator What It Means Expression Result eq is equal to \$x eq \$y False ne is not equal to \$x ne \$y True

You can also the greater- and less-than operators for string comparison - however, keep in mind that Perl uses the ASCII values of the strings to be compared when deciding which one is greater.

Assume \$x="m", \$y="M"

 Operator What It Means Expression Result gt is greater than \$x gt \$y True lt is less than \$x lt \$y False ge is greater than or equal to \$x ge \$y True le is less than or equal to \$x le \$y False

The reason for this - the ASCII value of "m" is greater than the ASCII value of "M".