Home arrow Perl Programming arrow Page 2 - Scalars and Boolean and String Operators

Comparing Numbers for Equality - Perl

In this third part of a five-part series on scalars in Perl, we continue our study of operators, moving on to Boolean and string operators. This article is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

  1. Scalars and Boolean and String Operators
  2. Comparing Numbers for Equality
  3. Boolean Operators
  4. String Operators
By: Apress Publishing
Rating: starstarstarstarstar / 2
April 08, 2010

print this article



The first simple comparison operator is ==. Two equals signs tells Perl to “return true if the two numeric arguments are equal.” If they’re not equal, return false. Boolean values of truth and falsehood aren’t very exciting to look at, but let’s see them anyway:

#!/usr/bin/perl -w
# bool1.pl

print "Is two equal to four? ",          2 == 4, "\n";
print "OK, then, is six equal to six? ", 6 == 6, "\n";

This will produce

$ perl bool1.pl
Is two equal to four?
OK, then, is six equal to six? 1

This output shows that in Perl, operators that evaluate to false evaluate to the empty string ("") and when true evaluate to 1.

The obvious counterpart to testing whether things are equal is testing whether they’re not equal, and the way we do this is with the!= operator. Note that there’s only one=this time; we’ll find out later why there had to be two before.

#!/usr/bin/perl -w
# bool2.pl

print "So, two isn't equal to four? ", 2 != 4, "\n";

$ perl bool2.pl
So, two isn't equal to four? 1

There you have it, irrefutable proof that two is not four. Good.

Comparing Numbers for Inequality

So much for equality, let’s check if one thing is bigger than another. Just like in mathematics, we use the greater-than and less-than signs to do this: < and>.

#!/usr/bin/perl -w
# bool3.pl

print "Five is more than six? ",      5 > 6, "\n";
print "Seven is less than sixteen? ", 7 < 16, "\n";
print "Two is equal to two? ",        2 == 2, "\n";
print "One is more than one? ",       1 > 1, "\n";
print "Six is not equal to seven? ",  6 != 7, "\n";

The results should hopefully not be very new to you:

$ perl bool3.pl
Five is more than six?
Seven is less than sixteen? 1
Two is equal to two? 1
One is more than one?
Six is not equal to seven? 1

Let’s have a look at one last pair of comparisons: we can check greater-than-or-equal-to and less-than-or-equal-to with the>=and<=operators respectively.

#!/usr/bin/perl -w
# bool4.pl

print "Seven is less than or equal to sixteen? ", 7 <= 16, "\n";
print "Two is more than or equal to two? ", 2 >= 2, "\n";

As expected, Perl faithfully prints out

$ perl bool4.pl
Seven is less than or equal to sixteen? 1 Two is more than or equal to two? 1

There’s also a special operator that isn’t really a Boolean comparison because it doesn’t give us a true-or-false value; instead it returns 0 if the two are equal, –1 if the right-hand side is bigger, and 1 if the left-hand side is bigger—it is denoted by<=>.

#!/usr/bin/perl -w
# bool5.pl

print "Compare six and nine? ",    6 <=> 9, "\n";
print "Compare seven and seven? ", 7 <=> 7, "\n";
print "Compare eight and four? ",  8 <=> 4, "\n";

gives us

$ perl bool5.pl
Compare six and nine? -1
Compare seven and seven? 0
Compare eight and four? 1

The<=>operator is also known as the spaceship operator or the shuttle operator due to its shape.

We’ll see this operator used when we look at sorting things, where we have to know whether something goes before, after, or in the same place as something else.

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: