First Steps / Basic operators
Subtitles of the Movie
Now we've looked at variables and the basic data types that we can include within them. Let's have a look at how we can manipulate them. An important element within the Perl language for manipulating variables, is the operator. In this movie we are going to look at different kinds of operators, and the way they work. First operator, one we've already run into, is the assignment operator. Now it looks like the equal sign that we used to for mathematics. But in the case of programming languages such as Perl we refer to this as "is set to". In these instances the variable age is set to 24, and the variable 'name' is set to Josh. So what the assignment operator does is to take a variable and then set it a certain value. We also have access to all the standard numeric operators we'd expect: addition, subtraction, multiplication, and division. These may look a little different from what you use to from mathematics: the plus and minus signs are very similar, but the multiply sign is more like an asterisk and the divide sign is a slash, rather than the divide sign that we use in mathematics. They all do exactly the same thing as we'd expect them to, however. 20 divide by 4. We can use these in combination of other operators, for instance to set the value of the variable 'a' to 45+50. What Perl does there is to evaluate this half of the expression here, which is 45+50, and it takes the number that results and passes that back to 'a' as it's value. We also have some extra numeric operators that we don't have, or at least we don't notate them in quite the same way in standard maths. Exponentation, raising a number to a power is represented by two asterisks. And we also have modulus. Modulus is for instance where we take two numbers and we attempt to divide one by the other, and the modulus will return the remainder. Here are some examples, 10%3 will return the remainder of 1. because 3x3 is 9 and then we can't fit another 3 in there, so whatever is left that's the value that returns. 2 to the power 5, we can work quite quickly up the binary scale to find to that: 2,4,8,16,32. Another operator which we should be aware of is the string concatenation operator. Now this is exactly like the full stop, and what it does is it runs two strings together. We can also use it to run a literal string or a variable or whatever we like. So in this case we are actually running four things together: we are running a literal string that says this is a, then we are running a variable here, then we are putting in just a space and then we are putting in another variable. Now of course we could have done all that without string concatenation operators if we wanted to use double quotes around the whole thing, but as an example this shows the kind of thing that we do could with the string concatenation operator. The string repetition operator is kind of similar, but what it does is that instead of adding two strings together, it repeats a single string the number of time we specify to the right of the string repetition operator. So 'W' followed by the string repetition operator followed by the number 3 will give us 'www'. But the second line here, we are asking the word 'why' to be repeated four times. As you can see we can happily mix operators within certain parameters, And Perl will return the appropriate response. So we're not asking 'Oh' and 'why' to be repeated four times, it just repeats the nearest value that it finds. We can also use a variable here to specify the number of times that we are going to repeat a string. In this case however 'many' would have to be set to a numeric value, otherwise this line of code wouldn't really make sense. So those are the basic operators that we could use. All the basic numeric ones plus the full stop or the string concatenation operator to run two strings together, and the string repetition operator which is simply the letter x, which allows us to repeat the string as many times as we specify.
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
United States 