Discover the major parts of a Perl script.
- View Offline
- In this lesson, we'll take a look at the parts of a common Perl script. Here's a working copy of Hello.pl from Chapter 3 of the exercise files. This is a very simple Perl script. I'm gonna go ahead and run it and you can see the output down there. It says "Hello, World!" It's not the simplest Perl script you can write, but it probably is the simplest Perl script that you should write. Let's take a look at what's here and why. Here on the first line, this is called the "Shebang Line." It is named for the fact that the exclamation mark is often called a bang and the line itself is read from the invoking shell.
So, "shell-bang" or "shebang." Perl was substantially inspired by Unix shell scripting and the shebang line is an old, Unix shell scripting tradition. When a Unix shell, such as bash or born, loads a script, it looks first at the first two bytes to see what type of file it is. If it sees these two characters in the position of the first two bytes, the hash mark and the bang mark, it will look at the rest of the line to find the shell to invoke in order to run the script.
Technically, this character is called the "number sign." It has many other names as well, including pound sign, and hash mark. I tend to call it a hash mark. The hash and the bang must be the very first two characters in the script and the path to the Perl executable must be all on the same line. If you're running Windows or another OS that doesn't use this convention, the shebang line will be treated as a comment line and ignored. It's a good idea to include it anyway in case your script is ever run in another environment.
Comments in Perl are line oriented. There are no multi-line comments in Perl. Comments are introduced by the hash mark and continued to the end of the line, like here on line 2. On line 4, you'll see "use 5.18.0." This specifies a set of syntax that's used for this script. The idea here is to allow the language to evolve without breaking older code. I tend to use 5.18 for everything. Perl 5.18 is old enough that I can be fairly confident that it is available on most systems, and my scripts will run just fine on systems with later versions of Perl.
Here on line 5, we see "use warnings." This is a pragma that tells the interpreter to issue warnings for potentially ambiguous or erroneous code. Perl syntax can be very loosely interpreted without this. This catches things like uninitialized values, unused variables, redefined variables, ambiguous values, and similar problems. Use warnings is widely considered to be best practice and I strongly recommend that you always use warnings in your Perl scripts.
And then finally down here on line 7, this displays a line of text on the console. So, first of all, "say" is the name of a built in function, and so it's not really a keyword, it's actually a function name, and it displays a line of text on the console. The term "console" generally refers to the text input and output on a computer display, like a hardware terminal, or a terminal program. Perl is a text oriented language and so it normally runs at a command line with a console.
In the case of our Komodo IDE, the console is the area at the bottom labeled "command output," and that's right down here. Notice that this line is terminated with a semicolon, that makes it a statement. We'll cover statements in more detail later in this chapter. Now let's close this and open a working copy of countlines3.pl from Chapter 3 of the exercise files. Perl is a very flexible language and this is not intended to imply that every Perl script will have the same look or even the same parts.
In fact, one of Perl's mottos is "there's more than one way to do it" and it's very true in the language of Perl. So, while I tend to write Perl like this, keep in mind that you'll see many variations of the same ideas in different people's scripts. First, notice down here on line 6, these previous lines, line 4 and line 5, are really pragmas and line 6 is actually reading in a module. So, it's reading in the IO file module.
Modules are a big part of Perl and you'll see a lot of modules used and there's a whole library of modules at cpan.org, or the Perl language. This imports the IO file module, which is used for reading and writing files, and we'll cover this in a lot more detail later in the course. We will cover both IO file and modules in general later on in the course. You notice here I call a function called main, which is defined down here at lines 11-16. This is a very common technique and again, this is a technique that I tend to use.
Some people put their main code right in line and not use a separate function. I tend to like to encapsulate it in a function. The "sub" keyword is used to define a function, and again I'll cover functions in a lot more detail later on in the course, and these curly braces are used to define a block of code, and so this block of code is associated with this function, and it contains the code for the main function. Functions are sometimes called "subroutines" in Perl because of the keyword "sub" to define them.
The terms "function" and "subroutine" are used interchangeably, at least in Perl and in this context. I tend to call them functions because in my mind this just rings more true. And then there's another function here for actually counting the lines and this was covered in more detail in Chapter 2 and another function here for error messages. And so, this is intended as a quick overview of Perl syntax. Keep in mind that Perl is very flexible so many scripts will look somewhat different than this.
But this is fairly typical, and we will get into a lot more detail of each feature as we continue in this chapter and in this 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