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: Intermediate:
A better version



Sep 25, 2001, 8:35 PM

Post #1 of 4 (1397 views)
A better version Can't Post

I have this script I've been working on that can be - in theory - embedded in an xssi call that recognizes the DOCUMENT_URI, queries that against a flatfile list and increases a count for that particular line. That all works fine in testing, but I am wondering a few things....

How would this thing handle simultaneous access? Does it need flock, and if so, where do I stick it?

As is, if the uri is equal to a stored variable of URL in the flatfile (multi-line, multi-entry) it increases the count, if not the only way I could get it to work without wiping the flatfile was by adding an else statement that does a print DATA $line;.

Does that mean that each time the uri does not equal the $url that it is rewriting the entire file over again?

Basically, I am interested in how to make this a better application, and about the flocking whole thing...

Any help would be apreciated, and here is the code so far:

#open and read data file 
@lines = <DATA>;

#start counting
foreach $line (@lines) {
($count, $section, $month, $year, $url, $title, $description) = split(/\|/,$line);
if ($ENV{'DOCUMENT_URI'} eq $url) {
print DATA ("$count|$section|$month|$year|$url|$title|$description");
else {
print DATA $line;

Thanks in advance


Sep 26, 2001, 12:55 AM

Post #2 of 4 (1392 views)
Re: A better version [In reply to] Can't Post

A it seams that you're opening a file, closing it and re-opening it in an other mode. This invokes with some nasty problems under UNIX.

You'd better sysopen() the file with O_RDWR, and use flock() to lock the file, so it won't be edited by an other program at the same time. Check the Fcntl module for that. Maybe you don't understand this now, but read the some manuals about it.

Theoretically, your an other program can attempt to read or write to the same file, while you were working on it. Appently, you assume nothing has changed in the file when you re-open it (thus overwriting new added data), or when both programs are writing data to the file (simultaniously under UNIX), your file gets corrupt!!!!

This concept is handled in some perlfaq item. (perldoc -q increment). It's called: 'I still don't get locking. I just want to increment a number in a file'

About the rest of your program, maybe you can do something with grep() or map() but I don't know that exactly.

(This post was edited by yapp on Sep 26, 2001, 12:31 AM)


Sep 26, 2001, 11:05 AM

Post #3 of 4 (1384 views)
Re: A better version [In reply to] Can't Post

By the way, this is kind of in reference to this thread

Where I was looking for a script that would do this, in the meantime I toyed around with the code in the first post, maybe it will help clear things up a to what I am trying to do....

I'm still looking through those items you pointed out, Yapp, and thanks for that...but I'm not finding something definitive, but I'll keep looking :)


Oct 1, 2001, 2:24 AM

Post #4 of 4 (1372 views)
Re: A better version [In reply to] Can't Post

You can also check this URL. The entire perldoc has been posted at the forum aswell


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

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