You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Rob Egan <ro...@artistdirect.com> on 2000/07/01 02:01:01 UTC

RE: can't properly append to file from mod_perl script

Hi,

I sent an earlier post with a script that was appending garbage along with
user email addresses that were submitted through a form. After seeing all
the suggestions about improvement, I went ahead and rewrote the script from
scratch (it's much shorter now!). My version actually prevents garbage from
being placed into the output file, but it still has a problem. If I open up
two browsers on separate machines, go to the page containing the form, and
simultaneously submit addresses from both machines, after maybe 8 or 9
entries the output becomes incorrect. For example, if I enter the following
e-mail addresses one at a time from the form:

user1@test.com
user2@test.com
user3@test.com
user4@test.com
user5@test.com

Then I view the output file and see this output:

user1@test.com
user2@test.com
user3@test.com
user1@test.com
user1@test.com

It's as though the parameters I'm pulling from the form get stuck somewhere,
but I can't figure out where. I tried autoflushing buffers for both STDOUT
and the output channel I use to write the output (called RESULTS in the
script), but that doesn't help. Some of you guys had mentioned writing some
cleanup code after I close my file, but I don't quite understand what I need
to clean up (sorry, I'm kind of new at this). Any ideas? (the code is below)

-Rob

--------begin script text----
#!/usr/local/bin/perl -w

# call strict, CGI, and Fcntl modules
use strict;
use CGI;
use Fcntl qw(:flock);
use FileHandle;

# autoflush buffers
$| = 1;

# Variable definitions
my $results_file = "./beastie.results.csv";
my $display_file = "./email_thankyou.html";
my $entry;

####----Sub routine definitions----Don't change anything below here----####

# General error routine (takes 3 string arguments, displays results in HTML)
sub bail {
  my ($status, $keyword, $message) = "@_";
  print h1("$status $keyword"), p($message), end_html();
  die $message;
}

# Open results file, lock it, write entry, close/unlock it.
sub write_entry {
  RESULTS->autoflush(1);
  open(RESULTS, ">>$results_file") || bail(500, "Results Error", "$!");
  flock(RESULTS, LOCK_EX);
  print RESULTS $entry;
  close(RESULTS);
}

# Display thank you page with link back to main site.
sub say_thanks {
  print CGI::header();
  open(DISPLAYFILE, "<$display_file") || bail(500, "Error", "$!");
    while(<DISPLAYFILE>) {
    print;
  }
  close(DISPLAYFILE);
}

####----Begin main program----####

# Create CGI object, and gather email addresses into array
my $query = CGI->new();
my $address = $query->param('email');

# Format URL encoded email address into ascii, append date stamp to it.
  $address =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C", hex($1))/eg;
  $entry = "$address, " . localtime() . "\n";

# Write the email and timestamp to results file, display thank you page,
exit.
write_entry();
say_thanks();
exit(0);

####----End of script----####