Perl Programming – Subroutine Arguments



Perl Programming – Subroutine Arguments

Perl Programming - Subroutine Arguments

Perl Programming – Subroutine Arguments
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
Hello everyone! Welcome back. In this lecture, we’re going to be taking a look at subroutine arguments. Again, this is section 5, lecture 5. Let’s jump right into it.
Subroutine arguments. What are subroutine arguments? Well, subroutine arguments are just values passed into the subroutine from the user. The user can pass in information into our subroutine that we can create and we can process that information in some type of form and give the results.
Why should we even use arguments? Instead of having to hardcode our programs and modify them every single time we need a new different value, we can give the user the choice to enter his or her information or values. We can collect information from our users and we can process that information again in our Perl program.
For example, if we have our subroutine called print_name. If I was the user, I would enter two pieces of information or two values which is my first name Victor and my last name Davis. I’m passing in two pieces of information. If we look into our print_name definition, if we go here, we’ll actually see a print statement. Hello it’s going to print out the first name and our last name. In this case, my first name and my last name. The user can enter their information that’s required by our subroutine and we can analyze and do some kind of operation on that information that the user enters. Let’s take a look at some of the examples now before I can show you guys live.
The first thing we want to do is we want to create our subroutine. Again I’ll use the keyword sub to start with our new subroutine followed by the name print_name. You know what? We actually do not need parentheses when we’re creating and defining our subroutine. I’ll just leave off the parentheses. Inside our block of code or our scope within our curly braces, I’ll type in parentheses. What I’ll do now I’m defining or declaring two new variables in a list. I’ll type first name, and for the second one I’ll type last name. Guys, here I’m creating scalar variables, two scalar variables as a list. As you can see the parameters define a list and I’ll only want to declare two scalar variables, first and last name. This right here, the @_ it actually defines the information of our argument. So I’ll explain this in more details in our next lecture, but for right now, we’re going to use the @_ to have our values from our arguments. Again, don’t worry about this for right now. I’ll explain that in detail in our next lecture. Let’s skip a line and just do a print statement. Put our famous double quotation marks. Let’s add a new line character. I’ll type Hello and I’ll just copy first_name. I’ll add a space and paste in the last name.
At the bottom of course we want to call our print_name subroutine. Now once we call it, we can add the parentheses we have to add the parentheses. This is because we’re going to add our information. The first name I’ll type Victor, last name I’ll type Davis. Even if we don’t have any parameters that’s required, we can still use empty parentheses. With me and if you’re an existing programmer, we always note to just put parentheses as just a habit with functions, methods and, in this case, Perl subroutines. I’ll save our information. Let’s run it and see if it works. Run Script. Congratulations! As we can see here, Hello, Victor Davis.
Again, what we did was we took user information, passed it into our print_name and our print_name subroutine had performed some task with our user information and displayed Hello then the name of the user. If you guys have any questions, please feel free to let me know. I’ll see you guys in our next lecture.

Comments are closed.