As my first post I decided to write about a language that fascinates me. It’s been quite some time since I finished reading Learning Perl, 2th edition from Randal Schwartz and Tom Christiansen, and I have to say I was amazed by this language. Now, this edition is rather old (1999) because I borrowed it from my local library, now I have the new edition (7th) on my library but the language didn’t change almost at all.

A few words about Perl: Perl is an old language. It was created in 1987 by Larry Wall. It was the standard language of the web two decades back. You can find more on its website and Wikipedia. I want to mention just two things: Perl is famous for its motto TMTOWTDI (“There’s more than one way to do it”) and that’s true. Some find it a bad programming habit, other don’t. Also, it is known as a “write-only” language because we can write easily programs that it’s impossible to read. Personally, I like the Perl way. As for readability, I think that depends on the programmer. Let’s see a very simple Perl program:


#!/usr/bin/perl

use strict;
use warnings;

foreach (reverse <>) {
	print;
}

That’s basically the UNIX tac program (without options) in Perl. For this example, we assume a UNIX-like operating system. Let’s break it down.

The line #!/usr/bin/perl defines the interpreter that the shell will use for executing the script. The location can be found with the command which perl as long as Perl is installed on our machine, which in the case of UNIX-like systems is the default.

The next two lines are optional. But their presence can help a lot as they warn about problems in syntax and compilation. Generally, one should include these lines in their program.

The next line is interesting. First, we have the special keyword foreach which takes a list as an argument and iterates through each of its elements which takes the value of a scalar variable that is following foreach. The syntax is

foreach $var (@list) {
    # perl code
}

We won’t talk about variables here, but a variable that is assigned to a single item is called scalar and has the $ sign in front of its name. Now, you may ask where is this variable? The Perl answer: it doesn’t have to be assigned. It’s not necessary. Every element through iteration is stored to the special variable $_. Every loop and other constructions in Perl that can assign items to single variables, they can take advantage of that fact and use $_ instead of user’s assigned variables.

OK, what about the list? We have inside parenthesis the strange format reverse <>. The reverse is a function that reverses a given list. Now, a word about functions (or subroutines as they are called in Perl). In many cases their arguments don’t necessarily need to be enclosed in parenthesis. We could also write reverse(list). The list in our cases is this strange operator, which is called the diamond operator. What this does is take as an input files that are passed as arguments in the program in the command line and read each line of each file. It then stores the reading lines in another special variable @ARGV. The symbol @ indicates that this is an array. (As we mentioned, we won’t talk about variables but think this as a list). If no files are passed as arguments, then the program waits for input by the user. Each line the user enters, is stored to the special variable @ARGV. The user can end his input by pressing Ctrl + D (end of file in UNIX).

So what this line does? Without arguments passed on, the program waits for user input. The user enters some lines, presses Ctrl + D, the different lines are stored in the array @ARGV and form a list. Next, the function reverse reverses the elements of the list and returns the reversed list. And with foreach we iterate through each element of the reversed @ARGV and store it to variable $_. I hope I didn’t confuse you :) It was a little mess for me at the beginning.

Lastly, what does the program do with each element? It just prints it. The program uses the function print which takes a list of items as an argument and prints them out. Remember that parenthesis is optional (not always) with functions. So where is our argument? Because our program doesn’t have an argument for the print function, Perl passes to print the default variable $_ and because it is taken for granted that we mean this variable, we just don’t even need to state it. Good, huh? And so, Perl prints each line we gave it in the reverse order.

Wow, that was longer than I expected. I hope that you find this explanation informative and motivate you to learn Perl or any other programming language. I’m a starter too, but I wanted to write this because I wanted to emphasize how beautiful programming is, even with such small programs. If you want to learn more about Perl you can visit this website:

Perl Library

It has great free material. Regarding books, Learning Perl is my recommendation to anyone who wants to learn more about this language or in general about programming.

OK, that’s it for today. Everybody be well and see you in my next post!