Thursday, March 31, 2011

Perls of Wisdom

Playing around some more with Perl I would like to share a few gems that I have discovered. This first comment will only impact any of a small number or users, but if you are programming on a *nix system using Emacs as your text editor you can use a little script to make the shebang appear in all of your perl files. Open up your .emacs file and insert the following code:

(setq auto-insert-directory (expand-file-name "/home/awestove/mytemplates/"\
))
    (setq auto-insert-query nil)
    (define-auto-insert "\\.pl\\'" "autoinsert.pl")
    (add-hook 'find-file-hooks 'auto-insert)

Basically what this is is elisp (emacs lisp) that sets up an auto-insert command. Every time you create a file with the .pl extension in emacs it will automatically insert the contents of autoinsert.pl into the file. Create the directory that you point to in the first line, and in that directory create your autoinsert.pl file. Inside of autoinsert.pl put your shebang ("#! /usr/bin/perl") and anything else you want in every perl file you create.

Sure this only saves a few keystrokes, but most programmers I know are quite lazy. I also created an alias:

alias ep "emacs /home/awestove/perl/\!*.pl"

This short alias means that all I have to type is "ep name" and emacs will open a file name.pl in my perl subdirectory with the shebang on the first line. The joys of automation.

Working some more with Perl I decided the next thing I wanted to focus on was some file I/O and conditional statements. Always good skills to have when learning any programming language, and Perl is no exception. All the better that Perl really simplifies the process of file I/O compared to other programming languages. The command is simply:

open(FILEHANDLE, "fileVariable");

When you want to access the file you use the FILEHANDLE. If you want to open the file for reading place a '<' before the file name, and use '>' for writing. There are a lot of ways you can use this, so I'll just start with the sample program I made to play with some file I/O:

#! C:/Perl/bin/perl
# ask the user to type the file name to read from
print "What file am I reading?";
my $fileIn = <>;
chomp($fileIn);
# and the file name to write to
print "What file should I create to write to?";
my $fileOut = <>;
chomp($fileOut);
# try to open the reading file, $opened is a boolean to signify if the operation was successful
my $opened = open(IN, "<$fileIn");
# unless is sort of an 'if not' statement, unless the file opened, print an error and exit
unless($opened){
     print "I could not open $fileIn";
     exit 1;
}
# if the file did open, try to open the output file
# instead of using an unless, we can just tell it to open 'or die'
# dying prints the given message and exits
open(OUT, ">$fileOut") or die("I could not create $fileOut");
# using the <> command on a specific file handle gets input from that file, instead of the default STDIN
# while loop continues executing as long as something is true, once the last line is read the loop will end
while($line = ){
     # get rid of the new line
     chomp($line);
     # a reg exp, if the line contains the number 1, print it to the screen
     if($line =~ /1/){
          print "$line\n";
     }
}
# the loop left us at the end of the file, we need to reopen to get back to the top
open(IN, "<$fileIn");
# @ means array, this reads the entire file into an array, one line per element
my @lines = ;
# printing to a file handle the sorted array
print OUT sort(@lines);
# closing our file connections
close(OUT);
close(IN);

Note the sort function for arrays, this assumes by default the array is characters, so it sorts based on strings. That is to say if your file was:

1
2
3
10
20
30

It would be sorted to:

1
10
2
20
3
30

All in all the use of file I/O and regular expressions is very simple and powerful with Perl. There is a reason the language is still popular for certain scripting projects.

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.