question on files and arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharpy
    New Member
    • Oct 2006
    • 1

    question on files and arrays

    i hope this is a simple answered question
    this program has been annoying me for some time, its pretty much the first thing i have attempted to do in perl. its a pretty simple guestbook type thing. it all works ok in that it saves all the newest information to a log file. however in falls down in the whole putting-the-old-data-back part. which is really quite a desirable function in my script.
    at the moment i have only four fields. the script is supposed to read the old data into an array, print the new data into the log file, then print the old data back after it, keeping the newest entries at the start of the file. eventually i plan to print the entries in all formatted in html then just use an ssi to paste the log file into another page. anyway! the script reads something out of the log file, and prints the new fields in no problem, however it only prints in the first line of the previous log file.
    so the log file only ever has 5 lines really.
    i have read asmany pages of perl documentation i can handle without breaking something, and they al seem to be telling me to do what i allready have.
    i have checked my server error log and nothing shows up. so its not really an error, just my code doing the rong thing.
    well here it is

    Code:
    #!/perl/bin/perl -wT
    use CGI qw(:standard);
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    use Fcntl;
    
    #assign web form to array:form
    my %form;
    foreach my $p (param()) {
        $form{$p} = param($p);
    }
    
    #open guest file for read/write, read data into array, set pointer to start of file
    sysopen (GUESTFILE, "guests.txt", O_RDWR | O_CREAT) 				or die "Couldn't open for reading: $!\n";
    my @data;
    @data = <GUESTFILE> || 0							or die "cant read from file $!\n";
    seek(GUESTFILE, 0, 0)							or die "cant rewind file $!\n";
    
    #print fields to file
    print GUESTFILE "First Name: $form{first_name} \n"				or die "cant print first to file$!\n";
    print GUESTFILE "Last Name: $form{last_name} \n"				or die "cant print second to file $!\n";
    print GUESTFILE "Country: $form{country} \n"					or die "cant print country to file$!\n";
    print GUESTFILE "Email: $form{email} \n"					or die "cant print email to file $!\n";
    
    #print earlier entries into file
    foreach $line (@data) {
    	print GUESTFILE ("$line") 						or die "cant print old file to file $!\n";
    }
    
    #close guest file
    close (GUESTFILE);
    
    
    #print html document to page, thank user, return to form page
    print "Content-type: text/html\n\n";
    print "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2//EN'>";
    print "<html><head><title>Form Response Page</title>";
    print "<META HTTP-EQUIV='REFRESH' CONTENT='5; URL=index.html'>";
    print "</HEAD><BODY bgcolor = '9900cc'>";
    print "<font color = ' ff0000'><h1><center>Thankyou! Your Guestbook Entry Has Been Added!<br>\n";
    print "Please Wait To Be Redirected To The Main Guestbook Page<br>\n";
    print "Or If You Are Impatient...<br>\n";
    print "<a href = \"index.html\">Click Here To Go Back To The Guestbook Page</a> \n";
    print "</body></html>"
    i can only assume theres something im doing rong in either
    Code:
    @data = <GUESTFILE> || 0
    or
    Code:
    foreach $line (@data) {
    	print GUESTFILE ("$line") 						or die "cant print old file to file $!\n";
    }
    ?
    or is there something im missing?
    something obvious probably.
    thanks in advance for absolutley any help =]
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Yes, you're right, this is easily fixed. The follow line of your code is your problem:

    Code:
    my @data;
    @data = <GUESTFILE> || 0 or die "cant read from file $!\n";
    The "|| 0" forces the read of the guest file into a scalar context, and so only a single line is read. I cannot think of a reason why this code is included, so it should just be moved, along with the "or die" statement.

    Code:
    my @data = <GUESTFILE>;

    Comment

    Working...