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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Time Stamp into a file



May 20, 2000, 12:50 PM

Post #1 of 4 (1319 views)
Time Stamp into a file Can't Post

This is my first ever posting to this forum, and also my first ever bit of Perl scripting. Basically I am taking data from a web form and writing the results into a text file which is then read later by a java app. Everything is fine except the time/date part. I want to ideally list the date as follows: [dd-mm-yyyy @ hh:mm AM/PM] then my text.

Here is the source code I am using, minus the lib.

use Time::localtime;

require "subparseform.lib";

$message = $formdata{'message'};

open (LOG,">infrastructure/data/ticker.txt") | | &ErrorMessage;
flock (Log, 2);
my $now = ctime();
print LOG "[$now] $message\n";
flock (Log, 8);
close (LOG);

print "Content-type: text/html\n\n";
print "<P>The message was: <BLOCKQUOTE><P><I>$message</I></BLOCKQUOTE>\n";

sub ErrorMessage {
print "Content-type: text/html\n\n";
print "The server can't open the file. It either doesn't exist or the permissions are wrong. \n";


May 21, 2000, 3:33 AM

Post #2 of 4 (1319 views)
Re: Time Stamp into a file [In reply to] Can't Post

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 &#0124; &#0124; 12, # transform 24-hour into 12-hour
$min, # minute
($hour < 12) ? "AM" : "PM" # AM or PM

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;

Or you could use the Date::Format module that's part of TimeDate - but then you'll need to install that module yourself first.

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

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") &#0124; &#0124; &ErrorMessage; # open file for updating
flock(LOG, LOCK_EX) &#0124; &#0124; &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) &#0124; &#0124; &ErrorMessage; # truncate the file to length 0 - this can fail too
print LOG "[$now] $message\n";

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 Smile
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;


May 21, 2000, 6:23 AM

Post #3 of 4 (1319 views)
Re: Time Stamp into a file [In reply to] Can't Post

Thanks a lot. I will give it all a try and let you know.


May 21, 2000, 7:01 AM

Post #4 of 4 (1319 views)
Re: Time Stamp into a file [In reply to] Can't Post

Thanks a lot, I have tested it and it all works! My book does not mention the CGI libarary, so I guess I am going wrong somewhere. The only other problem, I saw was the error I got when i left the "&#0124; &#0124;" in it was I missing something, or just being stupid???


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

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