IP

Sunday, December 19, 2010

Learn Perl in 10 easy lessons - Lesson 1



Thanks to the Perl scripting language you can automate various tasks in your Linux system. Learning Perl is both easy and fun, and you'll soon be able to write scripts which will make your life easier. In these series of articles I'll start by explaining the basics and I'll progressively introduce more complex concepts and advanced techniques. I'll try to explain things as much as possible, so whether you are familiar to programming or not, you should find it easy to learn Perl and be comfortable with using this language after the end of this series of articles.

History of Perl

Larry Wall created a scripting language in 1987, which he called the "Practical Extraction and Report Language". It was designed as a text-processing language for Unix operating systems. Various tools and languages already existed (Unix shells, sed, awk, C...etc) and programmers usually used many of them together. Larry Wall wanted the language to cover all aspects and needs related to text-processing so the programmer wouldn't have to use any other tool in conjunction with Perl. Also, he designed Perl as a language that was easy and fast to use but which would also allow the programmer to get into the innards of things.

Perl offered features no other language had offered before. It was filling a niche no other tool had, and it immediately became popular. In 1994, Perl 5 was released and at that stage it was a stable and well established general purpose programming language.
Particularities of Perl

Perl is truly unique. As we go along and study its different features you'll probably see that by yourself. Larry Wall applied a lot of his linguistic knowledge into the making of Perl. Some people even consider it a natural language. Its vocabulary is extremely rich and its grammar is very flexible. Perl programmers usually say "There's more than one way to do it". In fact, you can literally write a Perl script your way, with your own style. Some people even do "poetry" in Perl Because of this, some Perl scripts can be very difficult to read. Writing them though, is always a pleasure.
The Perl interpreter

Perl is an interpreted language. This means that the code you will write using the Perl language will need a program called the Perl interpreter in order to be run. For instance, if you write a Perl script in a file called myScript.pl (.pl is commonly used as an extension for Perl scripts), you will not be able to run it directly. You will need to call the interpreter to run it:

perl myScript.pl

In this example

myScript.pl

is your

Perl

script and perl is the Perl interpreter.
Installation of the Perl interpreter

The Perl interpreter is an essential tool and it is usuallyinstalled by default on most GNU/Linux distributions. Forinstance, the following distributions come with a recent and updatedversion of Perl:

* Suse 10.1
* Fedora Core 5
* Debian Testing
* Ubuntu 5.10
* Mandriva 2006
* Slackware 10.2
* Mepis 3.4-3
* Gentoo 2006.0
* Knoppix 5.0

For an exhaustive list of distributions which include perl, you can search distrowatch.com: http://distrowatch.com/search.php?pk....8.8#pkgsearch

To makesure the interpreter is installed on your machine and to see which version it is, you can open a terminal and type:

perl -v

If it is installed, this should print the version of Perl installed on your machine:

clem@pluto:~> perl -vThis is perl, v5.8.8 built for i586-linux-thread-multiCopyright 1987-2006, Larry WallPerl may be copied only under the terms of either the Artistic License or theGNU General Public License, which may be found in the Perl 5 source kit.Complete documentation for Perl, including FAQ lists, should be found onthis system using "man perl" or "perldoc perl". If you have access to theInternet, point your browser at http://www.perl.org/, the Perl Home Page.

If Perl is not installed on your system, you will certainly be able to install it through your distribution's package manager. Simply search for perl within your distribution's repositories, or start considering using another distribution. After all, Perl is an essential tool which should be included by default and regularly updated by your distribution: http://www.cpan.org/ports/#linux
Implicit use of the interpreter

The Perl interpreter is usually used to run Perl scripts which are written in a file. It also features an interactive mode which you can use by simply typing perl without any argument. However, in this lesson we will focus on using files.

In order to run a Perl script, you can call the interpreter with the file as an argument:

perl myScript.pl

... or you can tell the Perl script where to find the interpreter and make the script executable. This is common practice among programmers and you are encouraged to do so. Within the script file, the first line tells the shell how to interpret the file. This line basically shows the path to the Perl interpreter:

#!/usr/bin/perl

Note: The Perl interpreter is usually installed in /usr/bin, but your system might be different. To make sure, type "which perl":

clem@pluto:~> which perl/usr/bin/perl

Also make sure your Perl scripts are executable and have proper permissions:

chmod a+rx myScript.pl

Once the script is executable, it can be run directly. The shell looks at the first line of the file which starts with �#!�, and then it runs the interpreter which path is found on that line with the rest of the file. In other words, thanks to that little trick, you can run you Perl script directly:

./myScript.pl

Although you are not explicitly calling the interpreter here, keep in mind that it is actually run by the shell on your behalf and that it is the interpreter which runs your script.
Your very first Perl script!

You have your interpreter installed, and you're ready for your first script: a simple script that writes "Hello World!" on the screen (I know.. it's a bit useless, but it's more or less a tradition when you're learning a new programming language, so let's simply write "Hello World" and enjoy the lesson since it's still easy to understand ).

Create a file called helloWorld.pl and write the following into it:

#!/usr/bin/perlprint "Hello World! \n";

Make the script executable:

chmod a+rx helloWorld.pl

Run the script:

./helloWorld.pl

As you probably expected, "Hello World!" gets printed on the screen. The script only contains two lines and is quite easy to understand. The first line will always be the same; it tells your shell where to find the Perl interpreter. The second line is actually the only Perl instruction of your script, it tells the interpreter to print "Hello World!" on the screen. In Perl, each instruction finishes with a semicolon. If you're new to programming you'll probably forget semicolons in your code, so be careful with that. The reason for the semicolon is that an instruction can sometimes be long and take more than one line, so it is an effective way to mark the end of an instruction. Most languages use semicolons and once you're used to it, it seems very natural. You might also wonder what the "\n" is for. It simply is a special character which corresponds to the new line character (as when someone presses the Enter key) so that the cursor goes the next line of the screen after printing "Hello World!".

In the next lesson we'll start using variables, opening files and do a lot of things which will come handy for you later. Now that you know what Perl is and how to use it, we'll start to focus on the language itself. Of course, in the meantime I'll be glad to answer your questions.

No comments:

Post a Comment