The first Perl I wrote was to check my University's job web page for any jobs that had a higher pay than the one I was currently working. Since I was lazy, I coded up a quick and dirty Perl script to do this for me. All I had to do after that was to put it into cron and await its responses.
#!/usr/bin/perl
# NMSU Job grabber
# Matt Michie (mmichie@linux.com)
#----------------------------------------------------------------------
#Copyright (c) 2000, Matt Michie (mmichie@linux.com) (All rights reserved.)
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#
#Redistributions of source code must retain the above copyright notice,
#this list of conditions and the following disclaimer.
#
#Redistributions in binary form must reproduce the above copyright notice,
#this list of conditions and the following disclaimer in the documentation
#and/or other materials provided with the distribution.
#
#The names of this programs contributors may not be used to endorse or
#promote products derived from this software without specific prior
#written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
#CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#----------------------------------------------------------------------
#Instructions for use:
#
#You must have LWP installed to fetch the jobs. If you wish to use the
#e-mail notification you also must have sendmail installed.
#
#The script has the following command line flags:
#
#-v : Print the version
#
#-t : Max threshold to include the salary in statistics calculations
#
#-b (number) : Boundary of salary after which program will shoot off an
# e-mail
#
#-m (email address) : Tell the program to notify you with email. The arguement
# is a valid e-mail address.
#
#-q : Quiet mode, don't print statistics, automatically used in email mode.
#Example use:
#
#fetch.pl -b 7.50 -m mmichie@linux.com
#
#This tells the program to fetch the jobs list and only send e-mail
#notification if there are any jobs with higher pay than $7.50.
#----------------------------------------------------------------------
use LWP::Simple;
use Getopt::Std;
$version = 'Job Grabber 0.01';
getopts('vqt:m:b:') || die "Check your command line args!\n";
if ($opt_t != 0) {
$max = $opt_t;
}
else {
$max = 20; # Max threshold to include salary in count
}
$min = 0; # Min threshold to include salary in count
$highest = 0; # Highest salary
$total = 0; # Total jobs counted
$count = 0; # Total jobs which fall inside min/max thresholds
$oncampus = "http://www.nmsu.edu/~pment/oncampu.htm";
#$offcampus = "http://www.nmsu.edu/~pment/offcampu.htm";
$URL = $oncampus;
if ($opt_v) {
print "$version\n";
exit(0);
}
if ($opt_m && !$opt_q) {
$opt_q = true;
}
&fetch_page;
&stats;
if ($opt_m && ($highest > $opt_b)) {
&email;
}
elsif (!$opt_b && $opt_m) {
$opt_q = 1;
}
sub fetch_page {
unless (defined ($page = get($URL))) {
die "There was an error getting URL: $URL\n";
}
@page = split(/\n/, $page);
foreach $line (@page) {
$line =~ s/<[^>]*>//g; # strip HTML codes
if (!$opt_q && $line =~ /On campus job postings as of:/) {
print "$line\n";
}
elsif ($line =~ /SALARY:/) {
push @pay, (split (/:/, $line))[1];
}
}
}
sub stats {
foreach $elm (@pay) {
$total++;
next if ($elm <= $min || $elm >= $max);
if ($elm > $highest) {
$highest = $elm;
}
$count++;
$accum += $elm;
}
if ($count == 0) {
die "Eiiiiiiiiiieeeeeeeeeeeeeeeeeeee divide by zero :(\n";
}
else {
$avg = $accum / $count;
}
if (!$opt_q) {
print "Total jobs listed: $total\n";
print "Number of jobs counted for pay: $count\n";
print "Highest hourly pay: \$$highest\n";
printf "Average hourly pay: \$%.2f\n", $avg;
}
}
sub email {
open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
or die "Can't fork for sendmail: $!\n";
print SENDMAIL <<"EOF";
From: Job Grabber <$opt_m>
To: $opt_m <$opt_m>
Subject: Jobby
Total jobs listed: $total
Number of jobs counted for pay: $count
Highest hourly pay: \$$highest\nAverage hourly pay: \$$avg\n
EOF
close(SENDMAIL) or warn "sendmail didn't close nicely";
}
As you can see from the code, I wrote it procedurally, using as much of the C style syntax as I could. This increased my development speed, as all I had to do was look in the documentation to see where Perl's basic syntax differed from C's. Notice in one place that I actually used Perl's printf, since I was familiar with C's printf syntax. Also, I was able to use a couple of the excellent Perl modules to grab the web page off the Internet as well as process command line arguments.
You'll also notice that instead of writing e-mail code, I simply used the Perl script to throw data into Sendmail. This is a good example of how Perl glues together programs.
Eventually, to round out my toolbox, I will probably pick up Python or perhaps Ruby. Both of these languages have received wide acclaim for their clean syntax and object-oriented styles. Python is considered by many to be an ideal choice for a first language, because of its interpreted nature (making trying out new things quick), and the way it "forces" a clean style of writing code. If I was starting from scratch I would probably attempt either Python or C as my choice of first language.
|