
TheGame+
Deleted
May 21, 2000, 3:33 AM
Post #2 of 4
(349 views)
|
Hi, congratulations on your first Perl script. Here are a couple of comments to help you on the way : 1) the time/date part You don't really need to use ctime() and the Time::localtime module here. If you want to do your formatting by hand, use the values returned by localtime() : <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $now = sprintf("%02d-%02d-%04d \@ %02d:%02d %s", # the format you want to use $mday, # day of the month $mon+1, # month starts at 0 $year+1900, # year starts at 1900 $hour % 12 | | 12, # transform 24-hour into 12-hour $min, # minute ($hour < 12) ? "AM" : "PM" # AM or PM ); </pre><HR></BLOCKQUOTE> Otherwise, you can use the strftime() function of the standard POSIX module : <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> use POSIX qw(strftime); $now = strftime "%d-%m-%Y \@ %I:%M %p", localtime; </pre><HR></BLOCKQUOTE> Or you could use the Date::Format module that's part of TimeDate - but then you'll need to install that module yourself first. See http://theoryx5.uwinnipeg.ca/CPAN/data/TimeDate/Date/Format.html 2) the form parsing In general, it's better to use the standard CGI module for this : <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> use CGI qw/:standard/; # load standard CGI routines $message = param('message'); # assuming you have only one field called message </pre><HR></BLOCKQUOTE> 3) the file locking It's important to know what file locking can do, and what it can't do. On most systems, file locking is advisory instead of mandatory . This means that if your script (or java app) asks to lock a certain file, the system will check if there already is a lock on it, and wait until the file is unlocked by another app before giving you that lock. But if another script (or java app) doesn't ask if the file is locked, it will simply open that file, independently of whether the file is locked, being updated or empty at that time. So, ask yourself if ALL scripts and apps that will access that file will indeed ask for a file lock. If they don't, you'll have to find an alternative way to make sure no-one opens the file while it's being updated (not so easy to achieve) - or live with the fact that your java app may see an empty file for a fraction of a second... In this case, I don't think file locking is really essential - unless several people might update the file at the same time, using the same script. In any case, if you DO want to use file locking in some future project, do NOT use the code you showed here. Why ? Because the open(LOG,">...") will overwrite the contents of your file even BEFORE you ask for a file lock. Here's one way of doing better file locking : <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> use Fcntl qw(:flock); # import flock() constants open (LOG,"+<infrastructure/data/ticker.txt") | | &ErrorMessage; # open file for updating flock(LOG, LOCK_EX) | | &ErrorMessage; # get an exclusive lock - this can fail too #chomp($oldmsg = <LOG> ); # e.g. if you want to read something from the file first #seek(LOG,0,0); # not really important here, but needed if you read the contents of the file first truncate(LOG,0) | | &ErrorMessage; # truncate the file to length 0 - this can fail too print LOG "[$now] $message\n"; close(LOG); </pre><HR></BLOCKQUOTE> The close() will also flush the buffer and release the lock, so there's no need for an explicit flock(LOG, LOCK_UN) here. 4) the rest of the script Well, you already noticed above that there are several possible errors, even in a small script like this. So it's usually better to pass some message to your ErrorMessage subroutine, including the $! variable (which contains the exact error returned by the system). You could also use the standard die() function and add use CGI::Carp qw(fatalsToBrowser); at the top of your script to return fatal error messages to the browser. For the rest, you could also use the CGI module to print the necessary HTTP headers and HTML code, but I'll leave that for some other time Simply put, you'd use something like this (assuming you already have the 'use CGI' line somewhere above) : <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> print header; print start_html('Message Update'); #print whatever you want... print end_html; </pre><HR></BLOCKQUOTE>
|