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 reply to Re: Need to print file contents on page (updated) by haukex
in thread Need to print file contents on page by coolsaurabh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.