Perl/Hello World

In this tutorial, we are going to develop a simple 'Hello world' perl script with some nice features.

Prerequisites
You need a minimal understanding of the Perl programming language.

Note: While these scripts should run on Windows without modification (either Cygwin or ActiveState Perl), they are only tested on unix.

Classic version
Here's the classic version of 'Hello World':

Discussion
This just s the string "Hello, World!" plus a line-feed.

Ascii-Art version
For the Ascii-Art version, put the following text into a file called hello.txt (or draw your own):

We are going to read the file and output it on a line-by-line basis; more or less like the unix command 'cat' or the DOS command 'type'.

Discussion
The first commands  and   force us to declare all variables as my or our before using them and also help us identify other problems by either warning us or refusing to start the program altogether.

This is good, because it helps you to find problems very early on and also helps avoiding some obscure logic bugs like typing errors in variable names.

The command  opens a read-only filehandle we call '$fh' to a file called "hello.txt". If the open-command fails, we stop the program ('die') with an appropriate error message.

As you can see, the commands 'open' and 'die' are connected with an or command. That means, it will try to successfully run 'open' or 'die', starting with 'open'. That way, when we can't open the file, 'open' returns 'undef'; which for or is the same as 'false'. In that case, perl runs 'die'. If, on the other hand, open succeeds, it returns a filehandle (which of course is 'true'), and or doesn't have to run the 'die' command.

The next construct is our main loop. Most of the work is done by : For every iteration of the loop, we declare a local variable '$line' with my. At the same time, we initialize it with data from our filehandle '$fh'.

Because '$line' is a scalar variable (e.g. holds only one single value), we only read one single line from the file. With the next command  we output this line.

After finishing the loop (e.g. no more lines to read, '$line' gets 'undef', 'while' interprets that as 'false'), we close the file with  to free the file's resources.

Shorter Ascii-Art version
Now, we got a working prototype, let's shorten to code somewhat, to see how implicit variables work.

Discussion
The difference in this version is the main loop. More or less, we just leave out using our variable '$line'. As both the 'while' and 'print' command need a variable to work with - but we haven't given them one - they automatically use perl's built-in standard scalar variable called '$_'.

Note: While there are only a few uses where you'd actually need that behavior, it makes the code quite unreadable. Nevertheless, sooner or later you'll come across some program actually using this feature.

Super-short Ascii-Art version
Here's a really short version of the above program.

Discussion
This version leaves out most non-essential things including even basic code formatting.

First of all, we leave out all pragmas. So we don't get any advanced warnings (which is a bad thing). On the good side, we don't have to use 'our' or 'my' and so can save even more code.

In the open/die construct, we use perls built-in error variable '$!' instead of our own error message. Works quite well, actually. The main loop stays essentially the same, we just leave out some linebreaks.

Final advice
While the shortened code examples show all the "unneeded" things, they are not fit for production use! They may be ok for quick testing, but thats it.

Special advise from Cavac: Even when writing just a quick test or a proof-of-concept, always use 'strict' and 'warnings'; otherwise even a simple typo in a ten-liner might make you pull your hairs for hours.