Asked
Updated
Viewed
4.9k times

I'm having problems with my FFDB (flat file database). It writes to the database correctly from the form but I am having trouble getting my script to read the database and output the result. Here is the Perl source code I am using to attempt to read the data from the DB:

#!/usr/bin/perl -w

use strict;

open (INPUT, "file.txt") or die "Can't open data file: $!";

while (<INPUT>) {
    chomp;                  
    my @fields = split(/\|/, $_);
    print "$fields[1], $fields[0]: $fields[2]\n";
}

close INPUT;

The PERL script didn't work as a separate file with the shebang, so I integrated the script into the one that handles the form input from the website. The format of the FFDB has all of the words delimited by the pipe character. The script:

  1. Opens the FFDB for reading
  2. Then it loops through the data
  3. For each line it should print out the different fields

Thus, once I hit the submit button on the website, I should see the words printed out for each field. I have tried taking the print command out and putting in the field scalars into the HTML and it doesn't show the field scalars. Instead I get:

Internal Server Error

If I look at my log files it shows a little more information:

premature end of script headers

How can I echo out or print this text on a web page?

add a comment
0

1 Answer

  • Votes
  • Oldest
  • Latest
Answered
Updated

When and if you attempted to output this in your command console you would not have this specific problem. If you were having issues it was likely due to a different issue. However, if you are trying to get your Perl code to print this text out to your web page then you would have this error if you are not first sending the appropriate HTTP headers.

If you are wanting to get your script to print out to web page then before outputting any text you must first send the appropriate header with the PERL statement:

print "Content-Type: text/html\n\n";

As long as that statement has been executed somewhere before you print out to a webpage then this problem would not occur. However in your script if you have not executed this statement yet and you try printing out to a web page, you will not see anything or you might see Internal Server Error with your log mentioning premature end of script headers.

To be clear, change your code to:

#!/usr/bin/perl -w 

use strict; 

$header = "Content-Type: text/html\n\n"; 
print "$header";

open (INPUT, "db1.dat") or die "Can't open data file: $!"; 
while (<INPUT>) { 
    chomp;                  
    my @fields = split(/\|/, $_); 
    $html = "$fields[1], $fields[0]: $fields[2]\n"; 
    print "$html" 
} 

close INPUT; 
exit; 

The important part to note here is that the header must be sent before any actual text for your web page. If you attempt to send a header after output has already started, then you will have errors.

add a comment
0