coolsaurabh has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I am builing file name dynamically after taking user inputs and then from the file, need to print the contents on the same page in table format. I am getting struck because the code is able to create the file name but not able to print the file contents.Any help/advice will be highly appreciated.
use strict;
use warnings;
use CGI qw(:all);
my $cgi = new CGI;
my $this_url = $cgi->url(); #Save this script's url
my $first = $cgi->param('first_name');
my $last = $cgi->param('last_name');
my $file = $first . '_' . $last . '.csv';
open(DF,'<',$file);
foreach my $line(<DF>)
{
print $line;
}
close(DF);
print "Content-type:text/html\n\n";
print <<EndOfHTML;
<html><head><title>Generating Self-Referential URLs</title></head> <bo
+dy> <FORM action="Maintenance_Data.cgi" method="POST">
Circle: <input type="text" name="first_name"> <br>
Technology: <input type="text" name="last_name"> <input type="submit"
+value="Submit"> </FORM> <p>The name you entered was '$first $last'</p
+> <p>The name you entered was '$file'</p> </body>
EndOfHTML
Thanks,
Saurabh
Re: Need to print file contents on page (updated)
by haukex (Archbishop) on Jul 02, 2019 at 10:32 UTC
|
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.
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
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.
| [reply] |
Re: Need to print file contents on page
by roboticus (Chancellor) on Jul 02, 2019 at 11:56 UTC
|
your code
goes here
Now on to your question: The biggest problem I see in your code is that you're not bothering to check the results of the open call. I suggest you do something like:
open my $DF, '<', $file;
if (! defined $DF) {
print "Cannot open file '$file' because: $!";
}
else {
while (my line = <$DF>) {
print $line;
}
close $DF;
}
This way, you can get an error message that may provide you some hints as to what the actual problem is.
I suspect that:
- you're not specifying the correct path to the file,
- there may be permissions issues with the file and/or path, or
- the file doesn't exist.
...roboticus
When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] [select] |
Re: Need to print file contents on page (cgi101)
by Anonymous Monk on Jul 04, 2019 at 03:48 UTC
|
Hi,
You probably shouldn't allow arbitrary filenames, see WashFilename, use taint and launder dirty data ( WashFilename )
Also, obligatory, the checklists :) Basic debugging checklist , brian's Guide to Solving Any Perl Problem, CGI Help Guide , Troubleshooting Perl CGI scripts
cgi101 cgi102, more subs
(more subs , cgi101)
| [reply] |
Re: Need to print file contents on page
by NetWallah (Canon) on Jul 03, 2019 at 05:38 UTC
|
There are several issues with your code (besides formatting : Use <code> tags):
You should print Headers before any other operation - this allows you to report error messages if necessary.
Remember that the $file location is relative to the web root.
Error check your file open, and report accordingly.
print "Content-type:text/html\n\n"; # Or $cgi->header...
open(my $DF,'<',$file) or print $cgi->h1("Error opening $file:$!");
while (defined (my $line = <$DF>))
{
print $line, $cgi->br();
}
close $DF;
UNTESTED.
Update: I just noticed this is a dup of 11102294. Please reap main node and all replies including this one.
2019-07-28 Athanasius reparented
Time is an illusion perpetrated by the manufacturers of space.
| [reply] [d/l] |
Re: Need to print file contents on page
by AnomalousMonk (Archbishop) on Jul 03, 2019 at 05:34 UTC
|
| [reply] [d/l] [select] |
|
|