Home arrow Perl Programming arrow Page 3 - Scalars and Operators

Operators - Perl

In this second part of a five-part series on scalars in Perl, you'll learn about operators (both arithmetic and bitwise), among other things. This article is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

  1. Scalars and Operators
  2. Converting Between Numbers and Strings
  3. Operators
  4. Bitwise Operators
By: Apress Publishing
Rating: starstarstarstarstar / 1
April 01, 2010

print this article



Now we know how to specify our strings and numbers, let’s see what we can do with them. The majority of the things we’ll be looking at here are numeric operators (operators that act on and produce numbers) like plus and minus, which take two numbers as arguments, called operands, and add or subtract them. There aren’t as many string operators, but there are a lot of string functions. Perl doesn’t draw a very strong distinction between functions and operators, but the main difference between the two is that operators tend to go in the middle of their arguments—for example: 2 + 2. Functions go before their arguments and have them separated by commas.

Both of them take arguments, do something with them, and produce a new value; we generally say they return a value, or evaluate to a value. Let’s take a look.

Numeric Operators

The numeric operators take at least one number as an argument, and evaluate to another number. Of course, because Perl automatically converts between strings and numbers, the arguments may appear as string literals or come from strings in variables. We’ll group these operators into three types: arithmetic operators, bitwise operators, and logic operators.

Arithmetic Operators

The arithmetic operators are those that deal with basic mathematics like adding, subtracting, multiplying, dividing, and so on. To add two numbers together, we would write something like this:

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

print 69 + 118, "\n";

And, of course, we would see the answer 187. Subtracting numbers is easy too, and we can subtract at the same time:

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

print "21 from 25 is: ", 25 - 21, "\n"; print "4 + 13 - 7 is: ", 4 + 13 - 7, "\n";

$ perl arithop2.pl
21 from 25 is: 4
4 + 13 - 7 is: 10

Our next set of operators (multiplying and dividing) is where it gets interesting. We use the*and/operators to multiply and divide respectively.

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

print "7 times 15 is ", 7 * 15, "\n";
print "249 divided by 3 is ", 249 / 3, "\n";

The fun comes when you want to multiply something and then add something, or add then divide. Here’s an example of the problem:

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

print 3 + 7 * 15, "\n";

This could mean one of two things: either Perl must add the 3 and the 7, and then multiply by 15, or multiply 7 and 15 first, and then add. Which does Perl do? Try it and see . . .

Perl should have given you 108, meaning it did the multiplication first. The order in which Perl performs operations is called operator precedence. Multiply and divide have a higher precedence than add and subtract, and so they get performed first. We can start to draw up a list of precedence as follows:



To force Perl to perform an operation of lower precedence first, we need to use parentheses, like so:

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

print (3 + 7) * 15, "\n";

Unfortunately, if you run that, you’ll get a warning and 10 is printed. What happened? The problem is thatprint()is a function and the parentheses around3 + 7are treated as the only argument toprint().

print()as an operator takes a list of arguments, performs an operation (printing them to the screen), and returns a 1 if it succeeds, or no value if it does not. Perl calculated 3 plus 7, printed the result, and then multiplied the result of the returned value (1) by 15, throwing away the final result of 15.

To get what we actually want, then, we need another set of parentheses:

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

print((3 + 7) * 15, "\n");

This now gives us the correct answer, 150, and we can put another entry in our list of precedence:

List operators



Next we have the exponentiation operator,**, which simply raises one number to the power of another—squaring, cubing, and so on. Here’s an example of some exponentiation:

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

print 2**4, " ", 3**5, " ", -2**4, "\n";

That’s 2*2*2*2, 3*3*3*3*3, and –2*–2*–2*–2. Or is it?

The output we get is

$ perl arithop7.pl
16 243 -16

Hmm, the first two look OK, but the last one’s a bit wrong. –2 to the 4th power should be positive. Again, it’s a precedence issue. Turning a number into a negative number requires an operator, the unary minus operator. It’s called unary because unlike the ordinary minus operator, it only takes one argument. Although unary minus has a higher precedence than multiply and divide, it has a lower precedence than exponentiation. What’s actually happening, then, is-(2**4)instead of
(-2)**4. Let’s put these two operators in our list of precedence as well:

  List operators


  Unary minus



The last arithmetic operator is%, the remainder, or modulo operator. This calculates the remainder when one number divides another. For example, 6 divides into 15 twice, with a remainder of 3, as our next program will confirm:

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

print "15 divided by 6 is exactly ", 15 / 6, "\n";
print "That's a remainder of ", 15 % 6, "\n";

$ perl arithop8.pl
15 divided by 6 is exactly 2.5
That's a remainder of 3

The modulo operator has the same precedence as multiply and divide.

>>> 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: