good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Need to print file contents on page (updated)by haukex (Archbishop) |
on Jul 02, 2019 at 10:32 UTC ( [id://11102295]=note: print w/replies, xml ) | Need Help?? |
my $first = $cgi->param('first_name'); my $last = $cgi->param('last_name'); my $file = $first . '_' . $last . '.csv'; open(DF,'<',$file); First of all, note that this it is quite dangerous to use unfiltered user input to read a file. I would strongly recommend reading perlsec, enabling the -T taint switch, and filtering $first and $last for allowed characters. The problem might be that you aren't checking your open for errors, as in open(DF,'<',$file) or die "$file: $!";. This would give you an error message that you can see in the server's logs, or for debugging (not production!) you can add use CGI::Carp qw/fatalsToBrowser/; to the top of the script. In general, see CGI Help Guide and Troubleshooting Perl CGI scripts. Also, please use <code> tags to format any code, sample input, output, error messages, etc. Update: I missed this because of the missing formatting at first, but you're trying to write the contents of the file (print $line;) before you output the headers (print "Content-type:text/html\n\n";). You need to print any headers before the contents of the page, and since you're already using CGI.pm, you should use its header function instead of writing the headers manually. Also, while ( my $line = <DF> ) is generally better than foreach my $line(<DF>) because the latter reads the entire file into memory before looping over the lines. And using a lexical filehandle (open my $fh, ...) instead of DF would be better too.
In Section
Seekers of Perl Wisdom
|
|