Scripting with bash can help you with the automation of tasks and you can achieve a great deal by mastering bash scripting. However, your journey should not end with bash. While we have seen the power available in your bash scripts, we are limited by the commands we can run and the options they have. Bash scripts allow us to access commands; whereas, if we use Perl scripts, we have access to the programming interfaces or the APIs of the system. In this way, we can often achieve more with fewer resources.
In this chapter, we will introduce Perl scripting and some other basic scripts that we can use to learn Perl; we will cover the following topics:
Perl is a scripting language that was developed in the 1980's by Larry Wall to extend the functionality of sed
and awk
. It is an acronym for Practical Extraction and Reporting Language but has grown far bigger than its original purpose and today it is available on Unix, Linux, OS X, and Windows operating systems.
Although, it is a scripting language, it is not shell scripting; as such there is no Perl shell. This means that the code has to be executed via Perl scripts and not directly from the command line. The exception to this is the -e
option to the perl
command that can allow you to execute a perl
statement. For example, we can use the following command line to print the ubiquitous Hello World
:
$ perl -e ' print("Hello World
");'
You will find that Perl is installed by default on most Linux and Unix systems as many programs will make use of Perl in their code. To check the version of Perl that you have installed on your system you can use the perl
command, as follows:
$ perl -v
The output of this command is shown in the following screenshot from my Raspberry Pi:
If we create a Perl script, just like bash it will be an interpreted language and the first line will be the shebang, so that the system knows which command is to be used to read the script. The /usr/bin/perl
command can be used to locate perl
often. To verify this, you may use:
$ which perl
Unlike bash, when the perl
command reads the script it will optimize the script at runtime; one simple feature that this will enable is that we can define the functions at the end of the script, rather than before they are used. We will see this as we look at the Perl script in more detail, as we work through this chapter.
18.191.93.12