Perl Programming - Using variable length parameter lists
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, everyone! It’s always good to see you again in our lectures. In this lecture, we’re going to be using variable-length parameter lists. That’s our topic today, using variable-length parameter lists with subroutines. Again, this is section 5, lecture 6. Let’s jump right into it.
Using variable-length parameter lists. What is a variable-length parameter list? A variable-length parameter list is a list of values passed into the subroutine by the user which we’ve previously seen in our previous lecture. We have the user pass in some kind of information or values and we can process that information. For example, if I define a subroutine called add_numbers and let’s say if I want the user to add four different numbers, for example 1, 2, 3, 4, this is considered a parameter list. This is the list of values from the user that our subroutine can process. 1, 2, 3, and 4 are the values of the parameters passed into the add_numbers subroutine.
The subroutine actually has a special array called the @_. If we look at this, it’s just two characters the @ and _ together, this is the special name of an array of a subroutine that contains the multiple values of that subroutine. Again, 1, 2, 3, and 4 are the values of the special array called @_. That holds all the parameter values of that array that’s entered in from the user. Let’s do some examples before I can show you guys some action.
Let’s first define our subroutine. I’ll type sub and then I’ll call it add_numbers this time because I want to add some numbers. Again, if you’re defining your own subroutine, you do not have to add the parentheses at the top. We only add the parentheses when we call the subroutine. Let me go ahead and add a foreach control structure, followed by a scalar value. So for each element inside our array, we want to assign that value inside our scalar value. What I’m doing here, this @_ is a special array that’s connected to each specific subroutine. Again, this @_ is just a special array to the add_numbers subroutine and it’s going to contain the list of parameters that’s entered by the user, so it contains all of the values. What the foreach statement is going to do is going to look at each element value and assign it to our scalar variable called value. Again, let me finish with m foreach loop control structure. I just want to print each element value contained in our parameter list. I’ll just put the name of our scalar variable followed by a comma to print out our next value in our array or our next element in our array. Let’s now call our add_numbers subroutine. This time we add our parameter list in our parentheses. I’ll just add some numbers inside the parentheses, 1 2 3 4 5. These are my defined values that I want to put in as if I was the user. I save my work, click Run, Run Script. Let’s see what happens. As we can see, it printed 1 2 3 4 5.
The cool thing about a variable-length parameter list is that we don’t have to limit Perl to the amount of values or parameters that our subroutines need. We can add as many parameters that we want and give it to our subroutine. Perl does all the magic for us. Even if I added some more numbers, Perl will not give us an error because again this is a variable-length parameter list versus just normal parameters that’s defined either 1 2 3. But with our variable length, it doesn’t matter how many we add for our values in our parameters. I’ll go up, click Run, Run Script. It printed out the rest of our numbers. Again, I’ll re-explain what the foreach loop is doing. It’s looking at each element in our parameter list or our variable-length parameter list and it’s just displaying the values of those elements.
If you guys have any questions, please feel free to let me know and I’ll try to do my best to answer your questions. I’ll see you guys in our next lecture.