First Steps / Variables
Subtitles of the Movie
In this movie we are going to introduce one of the most basic building blocks of a Perl script. That is the variable. A variable is something which enables us to refer to a value without referring to it explicitly. We can just use a general name and then we assign a value to that name, and then Perl will interpret what we are saying and return the value. Let's have a look at a concrete example of a variable, as we might use it in the English language. A variable is like a box that we can put whatever we want into it. An example in the English language would be the word today. Now today in this instance is like the variable. But depending on what day of the week we are on that when we say today, the person that's listening to us say it will know that we are referring to a different day of the week. So for instance if it's Monday, then the value of today is Monday. Now let's take a look at how this translates into Perl. I am going to be using these two boxes here. This one is for code that we are going to write in Perl, and this one down here is going to be the output. So when we say - today is set to Monday - in this line, the value today is put inside the today variable. So, the variable today now has the value Monday. There's a couple of things that we should note about this line. Before the name of the variable, we put a dollar sign, and that's to tell Perl that the word just coming after the dollar sign is a variable. We are also using this sign, looks like an equal sign, when we see on it's own like that in Perl, it means 'is set to'. So whatever is to the left of the sign, the variable in this case, is set to whatever is to the right, which is the variable's value. So then, when we ask Perl to print the value of today's variable, it returns whatever value is set to it, in this case Monday. And it produces the value of the variable out of the box. Let's take a look, back to the text editor, and we will see an example of this in action. To begin with let's just get our Perl script, to output the literal string Hi. The string in this case is just a sequence of characters. So all we are asking it to do is something very simple, here similar to the script that we created in the previous chapter. And we are going to call this one hi.pl. Let's move over to our command prompt, and we are going to run the Perl script, and it produces the word Hi, so that's very simple. If we did this in a slightly different way, we could create a variable; and we are just going to call this variable 'a'. Perl knows it's a variable because we've used the dollar sign in front of it, and we will use the 'is set to' operator, and then we'll give it a value, 'Hi'. So when we come to run the print function, the print function is how we tell Perl to produce some output, we can tell it instead of printing Hi, we simply tell it to print the value of the variable 'a'. And that's what we are doing in this line down here. So let's save that, and move back to our command prompt. And as we can see this script does exactly the same as what it did before. So what's the advantage of using variables. The advantage is that, although we set the value of the variable 'a' to 'Hi', we can set it to something completely different, later on in that script. So now we have set it to something completely different. Then we can ask it to printout the value of the variable 'a' again; because it's changed let's see what it does. Let's save that and run the script again. As you can see, it's printed twice here, once it's printed 'Hi', and then it's printed 'Something else'. Because we haven't told it to produce a new line, it's run those two together. So you can see it's printing two very different values, even though the only print statements within our script both say print variable 'a'. So that's the basic element: the variable.
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 