Perl Programming – Working With Variables



Perl Programming – Working With Variables

Perl Programming - Working With Variables

Perl Programming – Working With Variables
Get the entire Perl Programming course for 20% off: http://stoneriverelearning.com/courses/perl-programming-for-beginners-online-course?product_id=38639&coupon_code=YOUTUBE20
Welcome back, guys. In this lecture, we’re going to be working with variables. Again, this is section 3, lecture 3.
So what are variables? Well, a variable is just a name for container that holds one or more values in memory in your computer or RAM. The name of the variable stays the same throughout the entire program during its run time or as long as the program is executing. But the value contained in that variable typically can change over and over again throughout the execution of the program. For example, if you want to modify the value, you can still use the same variable name with that different or existing value that’s been modified.
A variable holds of course single or more values. Variable names begin with a dollar sign $ in Perl followed by a given name. Let’s talk a little bit more about valuables. It’s called a Perl identifier. A Perl identifier is just a letter or underscore and then possibly more letters or digits or underscores that we can name our container or our variable name. So the beginning of a variable name cannot start with a digit or a number. Variables in Perl are always referenced with a leading dollar sign. The dollar sign just lets Perl know that this is going to be a variable to contain a value or more values. Of course you use the dollar sign to get the value of a variable as well. But it says to leave off the dollar sign, so assign a new value. But I’ve tested it and it gave me a compile error. So for right now, just skip the dollar sign off to assign a new value. We’re going to always use the dollar sign to retrieve values and to assign new existing values.
Again, variable assignment, you assign a value to the variable. The Perl assignment operator is just the equal sign =.
So interpolation of variables into strings. When a string literal is double quoted, it is subject to variable interpolation. This means that any variable name inside the double-quoted string will replace the variable name with its current value or its current values. Let’s go ahead and jump in and do some nice and fun examples.
Let’s begin by setting up and declaring some variables. Again, we start our variable with a dollar sign. We can name our variable anything but it cannot start with a digit. So let’s just name it variable_01. We want to set the value to a double-quoted string. This is variable 1. Of course we want to use our fancy new line characters to have it neatly outputted. Again, we’re using the equal sign to assign this value into our variable or container called variable_01. Let’s just copy a few of these lines and I’ll just rename it 02 and 03 for right now. Again, I’m just setting up some variables. Let’s say for variable_03, let’s just give this a number value, 88. I’ll do that and I’ll just print out some information. So we use our print statement and we want to see the value that’s inside or stored inside our container or variable that’s called variable_01. So we can copy that and paste it into our print operator. As we can see, we want to retrieve the value that’s why we’re using the dollar sign with our variable name. If we save our work and we click Run, Run Script, let’s see what happens. Look what we have. We printed out the value. This is variable 1. Let’s just try another one which is 03 which should just give us 88. If we click Run Script, there it is again, 88Press any key to continue.
So we know that if we use these variables, we can print out the values. Now let’s say if we put these variables inside double quotation marks. Let me just surround these by double quotation marks and I’ll put this one in as well. Let’s say variable 02. I’m going to ask Perl, “Hey, can you replace this variable name with its value which is set to 88, give it a space and replace this variable 02 with the value of This is variable 2?” So we save our work. We click Run, Run Script or F5. That’s exactly what it does. So again, we’re using variable interpolation. Like I said again, what that does is that we can use the variable name and it replaces it with its value. Let’s just say if we use single quotation marks. Let’s see what happens here. So if we save our work, click Run with the single quotation marks. Look at the big difference. Perl said, “Hey, since you actually only put the single quotation marks, I’m going to read your text as literal.” So it’s given as what we type. $variable_03 variable_02 is what’s going to be printed out because we’re telling Perl to print out literally what we type in. But again, the power of variable interpolation and the double quotes replaces the variables with its current value which is really nice and pretty nifty as well.