CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [sat110] Limit alerts to 3 max: Edit Log



FishMonger
Veteran / Moderator

Dec 21, 2008, 6:53 AM


Views: 5236
Re: [sat110] Limit alerts to 3 max

Kevin did a good job of showing how to limit it to 3 emails. So, now lets look at improving the overall quality of the script.

To begin with, every Perl script you write should include the warnings and strict pragmas which will point out common mistakes that are sometimes difficult to track down.

You could simplify the retrieval of the web page with LWP::Simple which uses LWP::UserAgent under the hood.

There really isn't any need to use the $found when checking for the date in the web page. Instead, move your checking of the count and email section into that spot.

Using the code Kevin gave is ok and is a common approach, but I think it could be improved with the use of the File::CounterFile module.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use LWP::Simple;
use File::CounterFile;

# Store the page in the string $webpage
my $webpage = get('http://www.yourdomain/test.html');

# Check if the requested webpage is there
unless ( $webpage ) {
# you might want to create a log entry
exit;
}

# the date is passed to the script as an argument
# and I really should add some error handling to this part
my $date = shift;

if ( $webpage =~ m|$date| ) {
my $counter = File::CounterFile->new('/fullpathto/alert-count.txt', 0);
if ( $counter->value <= 3 ) {
$counter->inc;

# personally, I'd use MIME::Lite to send the email
# but this method is common and ok.
my $mailprog = '/usr/sbin/sendmail -oeq -t';
my $recipient = 'test@yahoo.net';
my $sender = 'test@gmail.com';
open (MAIL, "|$mailprog");
print MAIL "To: $recipient\n";
print MAIL "From: $sender\n";
print MAIL "Subject: Schedule Page Updated or down\n\n";
print MAIL "NEW Basketball schedule may be posted.";
print MAIL "Must Call ASAP.";
close (MAIL);
}
}


Keep in mind that when you modify the date that you search for, you also need to reset the counter either by manually editing the counter file or simply by deleting the file.


(This post was edited by FishMonger on Dec 21, 2008, 7:13 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Dec 21, 2008, 7:13 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives