
FishMonger
Veteran
/ Moderator
Feb 12, 2009, 7:12 AM
Post #22 of 25
(16499 views)
|
Re: [sat110] Limit alerts to 3 max
[In reply to]
|
Can't Post
|
|
If you're going to use a boolean flag, there is no need to initialize it to 0. An uninitialized var holds a value of undef and in boolean context that equates to false. Personally, I would not use the flag. It would be better to correct the logic flow. In its current form, the key purpose of this script is to send an email if the following 2 conditions are met; 1) the counter is less than 3, and 2) the specified date is in the web page. If the counter equals 3, then there is no need to check for the date so therefore, there is no reason to download the web page. Checking the counter should be at the beginning of the script and the downloading of the web page should only be done if the counter is less than 3. When checking/incrementing the counter, instead of opening the file twice, it would be more efficient to only open it once and use seek and truncate when updating. However, personally I'd use the File::CounterFile module which does this for you and includes file locking. Since you're not setting a specific user agent, you could simply the retrieval of the web page by using LWP::Simple. Here's an updated version and since there's no indication that this is run in a cgi environment, I left out the loading of the CGI::Carp module.
#!/usr/bin/perl use strict; use warnings; use LWP::Simple; my $date = $ARGV[0] or die "Usage: $0 mm/dd/yy\n"; my $counter_file = '/path/to/alert-count.txt'; open( my $FH, '+<', $counter_file ) or die $!; my $cnt = <$FH> || 0; if ($cnt < 3) { seek($FH, 0, 0); truncate $FH, 0; print $FH ++$cnt; my $webpage = get('http://www.yourdomain/test.html'); unless( defined $webpage ) { die "failed to download schedule"; } send_email() if $webpage =~ m|$date|; } close $FH; sub send_email { # Write the alert-email my $mailprog = '/usr/sbin/sendmail -oeq -t'; my $recipient = 'test@yahoo.net'; my $sender = 'test@gmail.com'; open (MAIL, "|$mailprog") or die "can't pipe to sendmail $!";; 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); }
|