Home
Username:
Password:
Perl Fundamentals Tutorials

First Steps / More combined operators

Subtitles of the Movie

Now that's all very well for addition, we can also use a whole stack of other combined assignment operators to do similar things. The addition combined assignment operator will take a number, whatever the number of the value that the variable has, and then add whatever we put right at the combined assignment operator to the existing value of the variable. This one, the combined subtraction operator does a very similar thing, except that it takes whatever number we specify away from the value of the variable. The multiplication of course multiplies the value of the variable times whatever we specify to the right hand side to that operator and then returns that to the variable. Similarly with the division combined operator here. The modulus-combined operator does the equivalent for modulus and exponentation. Here's some examples: whatever 'a' is set to begin with, now 7 less. And then in the next line it's 10 time itself. And then on the third line we divide it by 2, whatever the remainder is, that's the due value that we give to the variable 'a'. So these are the numeric operators. Now we move on and take a look at another set of combined assignment operators, and these are the string ones. So here is the combined concatenation assignment operator, and the repetition equivalent as well. We take a look at some examples of the use of those. 'a' here, the variable 'a' has an existing value, and then in the first line we add some extra text to the value of the variable 'a'. And then in the second line we take the value of the variable 'a', lay it out 5 times, all run together and then we return that value, whatever we end up with and that's the value of the variable 'a'. There are a couple of other operators we should have a look at. And these are the increment and decrement operators. And now it gets even conciser still. We've seen that just by using standard operators, as in the first line, we are able to take the value of 'a' and add one to it, and make that the new value of 'a'. We can see also that we're able to do that using considerably less text here, by using the combined assignment operator. However, just in the case of number 1 we are able to do this in an even more concise way. We are just able to say a++ and that's the increment. And the increment here is able to add one to the variable, whatever the variable is, and set that as new value. Should draw a distinction here between post and pre increment operators. We can put the two pluses after the variable name or we can put them before. Lets have a look at what the difference is, when we do that. Let's set our variable 'a' again to 50, and we try saying print 'a++'. We are going to save this as increment.pl and move over to our command prompt, and we are going to run that script. Surprisingly we get the number 50. Now you might think from looking at this, these two lines are split here, that since we have added 1 to the value we should be getting the number 51. Well the difference between the pre and post increment operators is that this one, the post increment operator is as its name suggests executed after that line of code is evaluated. If we put it before and return to our command prompt, we will see that the increment is done just before the entire line is evaluated. So the number 51 is passed to the print function. This may seem like unnecessary complication and it's not strictly necessary for getting the job done. But you will find these very handy as we move through the course. Just for now it's enough to take note of them and to be aware of them, because you'll probably see them in a lot of Perl scripts, if you look at other examples. And so it's important to take note of them, be aware of them even if you are not using them straight up. We can also use the decrement operators. And these do exactly the same as the increment operators, except instead of adding one they take one away. The post and pre work in exactly the same way. So if we say a minus minus - that decrement is going to take place after whatever the statement is that's being evaluated. Whereas here first thing that happens is that we take one away from the variable 'a'. And then we execute the whole statement whatever that happens to be. So those are an overview of the basic Perl operators, and we've now looked at some of the basic building blocks for putting together Perl scripts.

Tutorial Information

Course: Perl Fundamentals
Author: Joshua Mostafa
SKU: 33403
ISBN: 1-9320-7215-2
Release Date: 2002-12-19
Duration: 7.5 hrs / 113 lessons
Work Files: Yes
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available