Monday, March 28, 2011

Learning Perl

Have you ever woken up and said, "Today would be a great day to learn a new programming language?" Today is one of those days and I have decided on learning Perl. Why Perl? Why not! Perl is an older language but still being developed and maintained, and is a dynamic language. Perl supports many different programming paradigms and is particularly powerful for tasks such as text parsing and code generation. For small scripts it is nice to have a basic language that you can just hack out some commands and get the job done, and Perl is an excellent choice for doing that. I am sure Perl has many more uses, but I am just learning it along with you right now, so we will discover those other uses when the time comes.

If you are working on a Macintosh or *nix system, chances are Perl is already installed, but for Windows users you will need to download and install it. I personally recommend ActivePerl which you can download from here. Just download the most recent version for your operating system and follow the instructions. When installing, make sure you check the option to add Perl to the Windows PATH variable.

With Perl installed it is best to follow tradition and start with a Hello World program. Using your text editor of choice create a blank document. The first line in all Perl programs is called shebang and should be something like:

#! usr/bin/perl for *nix
or #! C:/Perl/bin/perl for Windows

Since Perl is interpreted rather than compiled, this shebang points to the Perl interpreter. The rest of the code in the file is sent to the interpreter where it is run. In this program, we are only interested in doing one simple thing, printed a phrase to the screen, also know as standard output. To do this in Perl, like many other languages, the command is simply print followed by the string we want to print, using quotations for literals. So for our hello world program the full code is:

#! C:/Perl/bin/perl
print "Hello World!\n";

Nice and simple. Make sure you note the semicolon at the end of the print command and the lack of the semicolon after the shebang. This is because the shebang is not an actual Perl command, but rather a scripted instruction to the interpreter. Now, this is all well and good, but not overly useful, so let's add a few new things to our program. Some of the most useful things in a programming language are variables and input, so let's add those two features.

Instead of saying hello to the whole world, let's get personal and say hello just to the person running the program. In order to do this, we are going to have to ask there name using standard input. In Perl standard input is signified by <stdin> but by convention can be shortened to <>. To provide a prompt just use the print command. In order to save the answer the user gives us, however, we will need to create a variable, which in Perl is signified by a dollar sign ($). So, to ask the user their name and store it in a variable we would type:

print "What's your name?";
$name = <>;
print "Hello $name!";

Note that their is no need to concatenate the variable into the string literal, the use of the $ tells the interpreter that you want the value of the variable, not a literal $, to be printed to the screen. If you tried actually running this code, you might notice that the formatting seems off, specifically the program prints out a new line after the name before the exclamation point. This is because when the user typed their name, they hit the 'Enter' or 'Return' key, and that new line got saved in the variable as well. To remove this new line we can use chomp:

chomp($name);

Notice that you do not need to do $name = chomp($name); chomp actually modifies the passed in variable directly. With all of this our final program should look like:

#! C:/Perl/bin/perl
print "What's your name?";
$name = <>;
chomp($name);
print "Hello $name!";

And now we have a basic understanding of starting Perl. If you notice that the DOS window closes automatically without letting you see the program, either run the program from the command line, or add an extra <>; as the last line. This will stop the program from completing until you hit enter.

No comments:

Post a Comment