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.

No comments:

Post a Comment