Get a quick intro to functions and postfix notation.
- [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 pass it in array and this array is called ARGV. This is actually a special array that's predefined by Perl to contain the parameters that were passed from the command line when this script was invoked. It's common to pass it to main when you use this technique and you notice that I read from main. I read from the arguments with the shift function, right there, and I have this or operator and linesfile.text as a literal string.
All this does is it declares a variable called filename with my and it checks to see if there was a parameter pass. If it was, it uses that and if not, with this or operator, then it'll supply its own. Again, this is a very common technique for optionally reading a parameter from the command line. Now here, I call the countlines function and I pass it the filename and then I take the return value from that and I put in a variable called count and then I report there are countlines and filename.
I'll go ahead and I'll run this and you can see there's still 50 lines in the file and now let's take a look at the countlines function. You notice I have a comment above here, a couple of lines of comment, that declares how the function works. You don't actually have to read it if you just need to use it. You pass it a file name and it returns the number of lines. Here we take the filename again, using shift because you notice up here I passed it as a parameter to the function, and a different kind of a conditional you'll notice.
This is called a post-fix conditional. I have to object of the conditional, the actual command that happens first, so that it's obvious that this is an error message and I use unless filename. Unless the filename is defined, I'm going to go ahead and print this error message. Now, I open the file, you'll notice I'm using IO File again and again that is brought in up here using the use command, and so I open the file and here I use another type of logical or.
We'll talk about the differences between these two types, this one and this one. We'll talk about this later on in the course. This one is being used so that I know if I was not able to initialize this, then I can go ahead and print that error message. Then I count the lines using while and again, I'm using a post-fix while and so I have the count right up there at the beginning of the line where it's obvious what this line does. I increment count while I'm getting lines. That's very, very readable. These post-fix conditionals, these post-fix loops are for little, simple one-liners.
They're incredibly powerful and they're very, very readable. This is a great feature in Perl. I use it a lot and a lot of people do. Then we close the file and we return the count and then we have our simple error message down here where we display an error message in exit if we call error. It's very simple. Notice that if I change the name of the file so that it can't open, we'll get an error message here. It says, "Cannot open xlines.text. "(No such file or directory.)" That's coming from our error message down here.
$0, that is a built in variable, a default variable, that gives the path name of our script. Yours will be different, obviously, on your system and if I go ahead and undo that error, then we have our working script again. This is actually a very Perl-ish implementation of our countlines script. This is probably how I would write it if I was just writing a quick little utility. I've got countlines in a separate function so that it's modular. I'm using lots of post-fix conditionals and post-fix loops because those are easy and quick and very readable.
This is very Perl-ish. It uses many of Perl's features and we'll discuss these features in much more detail as we continue with the course.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices