Writing to files
Hello,
I have a script for keeping statistics. It writes some data to a file, but t runs a lot (I mean about 2 million times a day). How can I make this faster and what happens now if I can't open the file? Is there an error routine possible.
code:
#!/usr/local/bin/perl
($referrer, $window) = split(/\&/,$ENV{'QUERY_STRING'});
@land = reverse(split(/\./,$ENV{'REMOTE_HOST'}));
$time = time;
open (FILE, ">>/opt/guide/www.popupmoney.com/popuplog/popup.txt");
print FILE "$ENV{'REMOTE_ADDR'|$time|$referrer|$window|@land[0]\n";
close(FILE);
Can someone help me out?
Barry
myriad posted this at 09:33 — 30th August 2000.
They have: 88 posts
Joined: Mar 1999
Hope this helps
open (FILE, ">>/opt/guide/www.popupmoney.com/popuplog/popup.txt")|| dienice("PLEASE CONTACT SUPPORT - Can't open: $FILE - $!\n");
You may wish to lock the file while your writing to it:
# LOCK THE OPEN FILE
flock(file,2);
# RESET THE FILE POINTER TO END OF FILE BEFORE WRITING
seek(file,0,2);
You can put the following subroutine at the bottom of
your script:
sub dienice {
my($msg) = @_;
print "ERROR\n";
print $msg;
exit;
}
Regards
Steve
Vorm posted this at 21:38 — 30th August 2000.
They have: 62 posts
Joined: May 2000
Uh, is that REMOTE_HOST EV valid?
Also, shouldn't you be using the localtime function for the time?
[Edited by Vorm on 08-30-2000 at 05:44 PM]
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.