Home
Username:
Password:
Perl Fundamentals Tutorials

First Steps / Combined operators

Subtitles of the Movie

The operators that we looked at in the last movie were all pretty straight forward. A lot of them would be familiar to use already from mathematics. In this movie we are going to look at some slight more complicated assignment operators that are nevertheless very useful. They allow us to do things that would otherwise require the same set of operators to be repeated a lot of times, much more simply and precisely. So important that we get used to these slightly more complicated operators, if we are going to be able to read other peoples code and be able to code efficiently and concisely ourselves. Let's take a look to begin with, at these three lines of code here. The first line of code sets the variable 'a' to the number 3; the second line of code, we take the variable 'a' and we set it to a new value. And the new value is the variable a + 7. Every time we refer to the variable 'a' what we actually referring to, is the value that we've set it to. So in this case we are setting the variable 'a' to it's own value plus 7. So effectively we are increasing the value of the 'a' variable by 7. And then when we print it out, we will get a number - in this time 10. However that's using standard operators. IF we use a combined assignment operator we can write this in a much more concise way. This is the combined assignment operator: it looks like a plus sign followed by an equal sign, and what it essentially says is - take whatever value of 'a' is now and add 7 to it, and return that as the new value for the variable 'a'. Let's take a look at our text editor, so we can see how that works. We are going to create a variable called 'num', we are going to set that to number 30; then we are going to output something to our command line because that's where we are going to test these scripts - we are going to say the value of 'num' is, and then we are going to use the dollar sign here to show that we are actually asking for the value of the 'num' variable. Because we've used double quotes there, Perl is able to interpret the variable name that we've put within that string. Then we are going to say 'num' and then we are going to use the combined operator, we are going to use the same value again, 30. But what I am going to do, I am just going to copy this line; actually I'm going to put a line break character in there between them, so that it's more clear what's happening, [00:03:ds8 and then I'm going to paste it again after we've done this line of code here. So let's see what happens now: we are going to call this one combined.pl - let's pop over to our command prompt, and see what happens if we type in combined.pl. The two strings that are printed out show the two different values of 'num'. After we've just set 'number' to 30, that's all the value of num is. The second time however, we used the combined assignment operator. And that takes the existing value, which is 30, and adds 30 to it. And then it sets that new value to the variable. So as you can see, this is a very concise way of adding 30 to the variable 'num'. Without the combined assignment operator we'd have to type in some thing like this, which would do exactly the same thing. If we pop we back to our script and run it, we will see that same output is returned. It is a much more concise way, however, to simply use the combined assignment operator there.

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