Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to Need to print file contents on page

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.

Replies are listed 'Best First'.
Re^2: Need to print file contents on page (updated)
by holli (Abbot) on Jul 02, 2019 at 10:50 UTC
    And the OP probably cargo culted got that from some old tutorial which should have been forgotten years ago. And somehow it's always often noobies that appear here with crusty code like this. Is there too much bad information out there or is the good one too hard to find?


    holli

    You can lead your users to water, but alas, you cannot drown them.

      I feel that part of it relates to the popularity and ranking a lot of legacy content has, and the (often dubious) availability of old books. For people making minimum effort it's too easy just to cut and paste something old, which 'works', for various definitions of the term. Try phrasing some "How do I .... in perl?" type questions and check out the results.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11102295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 02:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found