Perl Programming - Private variables in subroutines
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 looking at private variables in subroutines. Again, this is section 5, lecture 8. Let’s dive right into it.
Private variables in subroutines. Private variables are only local to its scope or to its curly braces or its subroutine. Again, this is pretty much where your scope looks at its variable, for example, a subroutine. No other parts of the program can look at these private variables, only the subroutine.
Use the my operator to create private variables of the subroutine. For example, if we define a subroutine called add_numbers and we want to only have our variables be private or only to be seen only within our add_number subroutine, we start by using the keyword my followed by parentheses. This is where we add our list of variables or private variables. Again, x and y are my new private variables. Of course, what I’m doing here is whenever the user enters the first two values that the user enters, I want to use those and store those values inside my array or my subroutine special array. My special array of add_numbers is going to contain these two values by the user or private values by the user. We’re still within our curly braces of our subroutine so we can use and call these values x and y. Pretty much that’s all it does. It just lets us and allow us to create private variables that are only seen by the subroutine. Let me show you some examples in our demo now.
First we start by setting up our subroutine. I’ll call it add_numbers again, so add_numbers followed by my curly braces. I’ll use the my operator to start defining my private variables. I’ll use my followed by parenthesis and I want two different scalar variables that are private, so call it x and y. I want to assign it to our special array of our subroutine. So I’ll just use the @_, so my special array now is going to contain two elements which is x and y that’s assigned to our values of our elements in our array. I want to print the values inside our variables. I’ll put x = to my private variable called x and y = to my private variable called y. I’ll add a new line. I’ll add two new line characters this time. I’ll take my add_numbers subroutine. I want to call it for my first number I’ll put 50 and my second one 77. I save my work. Go up to my menu, click Run, Run Script and let’s see what happens. As we can see, it assigned our private variable 50 and reprinted 50 and the same thing for y but 77. Let’s actually do something a little bit more now to let you know that it is private. Let’s just say if we created another print statement to try to reprint those values, let’s see what happens. So add two new line characters to show you guys what’s going to happen. I’ll add x and I’ll add y followed by a new line character. Let’s see what happens. I click Run. Oh my gosh. Look what happens. Nothing actually happened besides the comma being printed. So the print statement and Perl saying, “Hey, I’m only going to print what I see which is the comma because you told me that these scalar values or scalar variables were private, so I’m going to do exactly what you tell me to do. So I’ll keep them private and we can only see them inside our add_numbers subroutine.”
Again, let’s say for example if I add another one here, let’s say if I want to modify them, let’s modify them. If we reprint them, let’s see what happens. As we can see, Perl is saying, “Hey, okay you included them inside your subroutine or within the scope which is just the curly braces and now I see them and now I’ll go ahead and print out their values for you.” So exactly as what we did here. We have 50 and 77 the first time. We’ve rewritten the data to 5 and 6 and we reprinted the values. Again, once it exits out of the subroutine and the curly braces, it doesn’t see those private variables anymore. If you guys have any questions, please feel free to let me know and I’ll see you guys in our next lecture.