Perl Programming - Using The Return Operator
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 using the return operator. What’s the deal with the return operator? This is section 5, lecture 7. Let’s jump right into it.
Using the return operator. The return operator immediately returns a value from a subroutine. What this means again is just that once our subroutine looks at the routine operator, it tells Perl to return that expression as a value or the calculation based on that expression and return it back as a value and to stop anything that the subroutine is doing after that return statement. We’ll take a look at that in examples as well.
For my example, I create a subroutine called names because once I find a name, I want to just return that name and stop any other code that’s being executed within our subroutine. I set up a foreach control structure. Of course, I want the user to type in some names. I store those names in my special array of my subroutine. I’m using another if control structure to find that specific name. If the name matches or is equal to Jaddian, I want to immediately return that name Jaddian if it was found in that list, in that parameter list. After that, it jumps down and jumps out of our subroutine. We want to assign that name a scalar value so we assign it to our scalar variable. We want to print that name. That’s all that I’m doing here. Let me do some examples for you guys and put this in action for you.
First, I start by defining my subroutine. That’s sub and this time I’ll call my subroutine names. Give it my curly braces to set up my Perl instructions or my block of code. What I’ll do here is I’ll set up my foreach statement or control structure. I’ll name our scalar variable control name and I’ll use my parameter list array because again I want the user to enter in a list of names. Once I do that, I want to add another if control structure this time. I want to compare two different values. I want to say if the current element value is equal—and again remember, guys, I’m comparing strings so I want to use the string comparison operator which is eq—followed by double quotes. I’ll just call the name Jaddian. I want to compare the value that’s contained inside that parameter list and I want it to be equal to Jaddian. If it is equal to Jaddian, I want to return that name. I’ll copy our scalar variable and that’s going to contain that name. Once it does that, it jumps out of our foreach loop and I want to call our subroutine this time. Let’s give it some names. I’ll call it the first parameter Victor, the second one Jaddian, and the last one Jackson. The reason why I wanted to find Jaddian because again it’s not the last name in our parameter list.
Once it looks at Jaddian, it’s going to stop looking at the elements in our array and just return the one that’s found only to let you guys know that once something happens and it finds it, it stops executing the block of code or the subroutine. Of course, I want to set up a scalar variable to contain our returned name and let’s just put here print Something is here. So I just kind of threw this print statement here to let you guys know that most likely it’s not going to use this print statement because like I said again, once it finds the return operator, it immediately tells the subroutine to stop doing whatever you’re doing and it just returned that value of that expression. Once we do that, we actually print out that name that it found, so I’ll just put Name selected. I’ll just copy and paste. This time I’ll put two new line characters. So let’s see what happens.
If we click Run, Run Script, it just gave us our name. Again, let me see what happened here. Undefined subroutine. Sorry about that, guys. If you guys find errors, they’re your best friends because you learn a lot more when you happen to throw in errors. As we see Perl is helping us out by saying, “Hey, you’re error is on line 13. You have an undefined subroutine called name.” If we look back, of course I missed the s. If I just put in the s, resave our work, click Run, Run Script. It should work out as what we expected. Again, what it did was once it found the return statement, it immediately saves our value and it did not jump down to our next line of code which is print because again by definition, once it finds the return statement, it immediately returns that value or it exits out of that block of code and most likely your subroutine as well.