From the course: Perl 5 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Functions

Functions - Perl Tutorial

From the course: Perl 5 Essential Training

Start my 1-month free trial

Functions

- [Voiceover] In this lesson, we'll use a function to read lines from a file and we'll learn a few more essential Perl features. Here's a working copy of countlines3.pl from the Chapter 2 of the exercise files. In Perl, a function is sometimes called a sub-routine. The two terms are used interchangeably, mostly for historical reasons. I mostly use the term function. A function is simply a reusable block of code that may accept parameters and may return a value and has its own scope. We'll get into a lot more detail about functions later in this course. For now, here's a few important points about functions. A function is declared with the sub keyword. You'll notice here on line 11, the sub main, that defines that function main. Parameters are passed as an array of scalers and may be accessed with the shift keyword. You notice here that I do this here. This is the main function. It's a come technique for keeping code organized. Notice that I call main on line 8 and when I call it, I…

Contents