Perl Programming - Subroutine Definitions
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 subroutine definitions. Again, this is section 5, lecture 2. Let’s jump right into it.
Subroutine definitions. Before I’ll explain all this good stuff about subroutine definitions, we’ve previously had been working with built-in Perl subroutines. For example, the print subroutine which displays information visually on our screen that uses standard output. Again, for you previous programmers out there, instead of the name subroutine, they’re usually called methods or functions in other programming languages such as C, C++, and Java. But in Perl, they’re called subroutines.
What is a subroutine? A subroutine is nothing more again but a group of Perl instructions or statements to carry out a specific task. We’re going to actually define our own subroutines in Perl. In order to do this, we can use the keyword sub followed by the name of the subroutine. The name of the subroutine we still have to follow the rules of the Perl identifier just like defining variables. Then the indented block of code within curly braces. Within the curly braces, that’s where our Perl instructions will go to carry out the task or our Perl statements.
This is a little quick example of what a defined subroutine or user-defined subroutine looks like. Again, we have our keyword sub. Then we have our Perl identifier name or our subroutine name that we can define. I used a mixture of both letters and underscore, so print_name is the name of my subroutine followed by a pair of parentheses. At the bottom, I have my opening and closing curly braces. All I want to do is to carry out an instruction called print. Print is a built-in Perl subroutine as well.
Pretty much in a nutshell that’s the definition of a subroutine. We can define our own subroutines and we can use Perl built-in subroutines as well. If you guys have any questions, please feel free to let me know. I’ll see you guys in our next lecture.