My First Perl Program
In college, we wrote all of our programs in Java. That's just what the Engineering School decided, and anyone who was an undergrad with me (1998-2002) had to learn Java programming. There was no other alternative, and this is still the curriculum today.
However, there was always that one jerk in our class who would write his homework in Perl. Much like mathematicians always talk about elegant proofs, in computer science we always talk about elegant code. (This is really a great annoyance for true engineers who just want the darn thing to compile and run). Perl is not particularly elegant, but is extremely practical and efficient. So, this Perl jerk would always submit code that was, on the average, about 90% shorter than everybody else's java code. The professor would chastise him for not following directions, but I know that he was secretly grateful to have one less hideously intricate homework to debug.
As I read Michael Schwern's manifesto - Why I am Not a Java Programmer - I experienced the following thread of thought:
- Where the hell do you get off!
- Ugh... "hello world" SHOULD be one statement.
- I am thirsty.
- If there's more to life than OO, I dare you to tell me what it is!
- Oh.
- Yea, you are right.
- I should really learn Perl. I'm such a dork.
So, with that, I decided to take just a little bit of time to write my first Perl program. (I should mention that next semester I am taking yet another class in Java Programming. Java is really good for the development of visual and interactive interfaces, which is what I need to do for my grad work. So, this is a total diversion, but it's a fun one at that).
Perl was originally designed for text processing, and that is the direction I took with my first attempt - I decided to write a simple (very simple) rhyming assistant. I used the scrabble dictionary file (TWL98) because it is more comprehensive than the built-in Unix dictionary. Here's the commented code.
#!/usr/bin/perl
use strict;
use warnings;
# Dictionary file to use
my $file = '/lib/scrabble/TWL98-alpha.txt';
# Get match from command line or exit with explanation
my $match = shift @ARGV or die "$0 MATCH_STRING\n";
# Open the file, or exit with explanation
open ( my $FH, $file ) or die "Cannot open $file: $!\n";
# Read lines of the file into $line
while ( defined ( my $line = <$FH> ) ) {
# Strip trailing whitespace (\s) characters
$line =~ s/\s+$//;
#Print the line if the line ends in the string
#we're searching for. The match is case-insensitive
print "$line\n" if ( $line =~ /$match$/i );}
# Close the filehandle
close $FH;
Basically, the program takes input, and searches the dictionary file to find other words that end in the same string of letters as the input. (As you can see, I had to do some text manipulation on the dictionary file, but if you use the nicely formatted Unix dictionary, you can skip that part). Here is an example of how it runs.
During a "math songs" concert, Mr. Tom Lehrer said that he couldn't find any words to rhyme with algebra (thus, no songs about algebra), and issued a challenge for anyone to find a rhyme for it. So, I ran this rhyming assistant on the suffix "ebra" to see if I can get any matches:
$ ./dict.pl ebra
ALGEBRA
CEREBRA
PALPEBRA
VERTEBRA
ZEBRA
You can get more matches if you run it on "bra" instead of "ebra" as follows:
$ ./dict.pl bra
ABRACADABRA
ALGEBRA
BRA
CANDELABRA
CEREBRA
COBRA
DOBRA
LABRA
LIBRA
PALABRA
PALPEBRA
PENUMBRA
SABRA
UMBRA
VERTEBRA
ZEBRA
So, there you go - only 10 lines of code. Perl really is pretty neat (pun intended), and I'm well on my way to becoming a convert. But, I don't think that I am ready to abandon my Object Oriented ways just yet.


0 Comments:
Post a Comment
<< Home