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 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 print "21 from 25 is: ", 25  21, "\n"; print "4 + 13  7 is: ", 4 + 13  7, "\n"; $ perl arithop2.pl 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 print "7 times 15 is ", 7 * 15, "\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 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 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 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 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 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 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 print "15 divided by 6 is exactly ", 15 / 6, "\n"; $ perl arithop8.pl The modulo operator has the same precedence as multiply and divide.
blog comments powered by Disqus 






