Thread: Perl code
View Single Post
Old 08-23-2002, 06:22 PM  
fiveyes
Confirmed User
 
Join Date: Aug 2001
Location: New Orleans
Posts: 1,680
Ahhh, perl!

Armed & Hammered's suggestion works but I don't feel it's that good. The reversal of data would require a SSI call to a program to do it and whatever savings in adding an item would be more than offset in the overhead involved everytime the document is viewed. This holds unless you actually have more people adding data than simply viewing the listing, which is unlikely.

NetRodent's response is better, in that it concentrates the resource usage to only when adding a new listing and then simply echos the list when it's viewed. However, the "print" statements are not going to the file handle he opened (OOPS!), one should always check for the failure of file operations and, most importantly, there has to be a way of locking the code in a CGI enviroment when you're doing file operations. Otherwise, you run the risk of two seperate processes colliding and the list will be corrupted from that point forward.

Code:
    $file = '/path/to/list.txt'; 

    open(FILE, "+< $file") or die "can't open $file: $!";
    flock(FILE, 2)

    @lines = &ltFILE>;

    # Write the new addition first
     (print FILE "$newdata\n") or die "can't write to $file: $!";
    # Copy original to new
    while (@lines) {
	(print FILE $_) or die "can't write to $file: $!";
    }

    # No need to explicitly unlock,
    #  "close" does it for us
    close(FILE) or die "can't close $file: $!";
__________________
<CENTER><A HREF="http://www.hot-off-bourbon.com/" target="_blank"><IMG SRC="http://www.hot-off-bourbon.com/images/hob-logosmall.jpg" border="0"></A>

<FONT face="Comic Sans MS" SIZE="-1"><I>Mardi Gras, Spring Break, Wet-T, Night Club Action, UpSkirt, Oil Wrestling, Voyeur</I></FONT></CENTER>

Last edited by fiveyes; 08-23-2002 at 06:25 PM..
fiveyes is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote