spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / perl / tutorial / 3

Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

PerlHoo, Part II

Storing User Recommendations

Now we must deal with a few issues related to storing new site recommendations. When a user wants to recommend a new site for a particular category, he/she clicks the link labeled Suggest new link at the bottom of the directory page. This link is created dynamically in the print_footer subroutine (Lines 152-157). This calls the print_add_screen subroutine (Lines 99-122) which prints the form. The form is similar to the one below:

URL:
Title:
Description:
Your Name:
Your Email:

When the user completes the form and clicks the Submit Resource button, the data is sent back to the script. Assuming the form method is POST, the add_link subroutine (Lines 55-91) appends the information to $rootdir/$reldir/$new_datafile which in our case would be /www/directory/Computers/Perl/perlhoo_new.csv if the category was Computers/Perl.

File Permissions

Because we're writing to files now, we must make sure that our Web server has proper permissions. Make sure the PerlHoo directory defined in the $rootdir variable on Line 32 is writable by your Web server.

File Locking

The add_link subroutine stores new site recommendations in a CSV file when the user clicks the Submit Resource button. This can cause a race condition where multiple users submit a new site simultaneously. In such a case, only the second set of data would be added to the file.

To solve this potential problem, add_link requests an exclusive lock on the file before it writes to it. This will ensure that data will not be lost. In Line 60, we open the data file in append mode. Then we request an exclusive lock in Line 61. We combine the form data into a CSV record in Lines 63-65 and write it to the file. The lock is released when the file handle is closed in Line 66.


home / experts / perl / tutorial / 3

http://www.internet.com

Produced by Jonathan Eisenzopf and

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji


Created: April 20, 1999
Revised: April 21, 1999

URL: http://www.webreference.com/perl/tutorial/3/