[LA.pm] See if this sparks some perl talk.

Benjamin J. Tilly ben_tilly at operamail.com
Mon Oct 18 20:03:36 CDT 2004


Nicholas Bernstein <nick at docmagic.com> wrote:
> 
> I needed to throw together a script to pull the redsox scores down from
> a website and text message the scores to a friend. We have a tradition
> of doing this for friends when they can't watch the game, and I tend to
> forget to keep sending scores for innings at a time. :) 
> 
> Anyway, since I threw this together rather quickly, didn't put enough
> effort in to be embarrassed by it, I figured maybe it might be a good
> way to spark up the list a bit. Critique, improve, extend, replace, turn
> into something else, etc. 

I'll just state the glaring stuff then.

You should use strict and declare variables with my.  As stated in
perlstyle, you should test for failure of your system calls and
emit errors:

   open(FILE, $file) or die "Cannot open '$file': $!";

I notice that you use ($foo == 1).  A good habit is to reverse that
and write (1 == $foo).  That way if you accidentally type = instead of
==, you get a syntax error rather than always succeeding by wiping out
$foo.  (Warnings would catch that typo as well.)

> Right now, it's run from cron, and works. :) The log page is generated,
> and you can use the older URL to view an example of what I'm scraping.
> The one in the example should work fine.

None of the things that I cited change whether it runs.  They do
change how confident you are that it runs _correctly_.

Cheers,
Ben

> -nick
> 
> 
> ###------------------------ start script
>         
>         #!/usr/bin/perl -w
>         use warnings;
>         #use Mail::Sendmail;
>         #use diagnostics;
>         
>         $^W=0;
>         $debug = 0;
>         
>         =pod 
>         
>                 This script scrapes the boston.com website for the scores to 
>                 Boston redsox games. It's not very dynamic, You'll need to change the opponenent 
>                 
>                 Page can be found @:
>                 	http://boston.com 
>                 		-> sports
>                 			-> redsox
>                 				-> log
>                 eg usage:
>                 ./scores.pl 'http://146.145.120.3/merge/tsnform.aspx?c=globe&page=mlb/scores/live/log.aspx?GameID=11880'
>                 
>                 
>                 Mail:;Sendmail:
>                 	perl -MCPAN -e "install Mail::Sendmail"
> 
>         =cut
>         
>         $url 		= "\'" . $ARGV[0] .  "\'" ;
>         $mail_app	= '/usr/bin/mail' ;
>         $have_score 	= 0 ;
>         @page 		= `lynx -dump $url`; 
>         $email 		=  '8675309 at vtext.com, me at nicholasbernstein.com';
>         $regex		=  '(NY Yankees \d+, [Bb]oston \d+)';
>         
>         
>         if ( defined($ARGV[2]) ) { 
>         	$log = $ARGV[2]; 
>         } else { 
>         	$log = "/tmp/scores.pl.log";
>         }
>         
>         print "$log\n" if ( $debug == 1 ) ;
>         
>         foreach ( @page ) {
>         	if (	( $_ =~ m/$regex/)   
>         		and ( $have_score == 0 )) { 
>         			$score = $1;
>         			chomp($score);
>         			if ( -f $log ) { 
>         				print "In log exists\n" 	if ($debug==1);
>         				open("LOG", "<$log") 		or die("problem opening file\n");
>         					@old_score = <LOG> 	or die("could not read log\n"); 
>         				close("LOG");
>         				print "\nLog: $old_score[0]\n" 	if ( $debug == 1 );
>         				$old_score=$old_score[0];
>         			} else { 
>                                         print "In log does not exist\n"	if ($debug==1);
>         				open("LOG", ">", $log);
>         					print LOG "none";
>         				close(LOG);
>         				$old_score = "none";
>         			}
>         
>         			chomp($old_score);
>         	
>         			if  ( $score ne $old_score ) { 
>         				open(MAILAPP, "|$mail_app -s \"RedSox Score\" -r \"scores\@nicholasbernstein.com\" $email");
>         					print  MAILAPP "$score" ;
>         				close("MAILAPP");
>         				print "$score\n";
>         				open ("LOG", ">", $log);
>         					print LOG "$score\n";
>         				close("LOG");
>         				print "Scores are different"	if ( $debug == 1 );
>         			}
>         			$have_score = 1 ;
>         	}
>         }
> 
> ###------------------------ end script
> -- 
> Nicholas Bernstein, Unix Systems Administrator
> Document Systems Inc.
> http://docmagic.com
> nick at docmagic.com
> 
> 
> CONFIDENTIALITY NOTICE: This email and any attachments thereto may contain information which is privileged and confidential, and is intended for the sole use of the recipient(s) named above. Any use whatsoever of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by persons other than the designated recipient(s) is strictly prohibited. If you have received this email in error, please notify the sender either by telephone, by email, or by any other written or telephonic means, and delete the material from any computer. Thank you for your cooperation.
> 
> 
> _______________________________________________
> Losangeles-pm mailing list
> Losangeles-pm at mail.pm.org
> http://mail.pm.org/mailman/listinfo/losangeles-pm
> 




More information about the Losangeles-pm mailing list