PerlHoo, Part II | 6
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:
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
Theadd_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.
Produced by Jonathan
Eisenzopf and
Created: April 20, 1999
Revised: April 21, 1999
URL: https://www.webreference.com/perl/tutorial/3/