Most revered monks,
Lately I have been plunging myself into Perl's CGI, programming. And stumbled myself on some aspects of it which I can't seem to figure out the problem. Thus to you I turn for enlightenment....
I was trying to do a file upload through UploadFile() method in CGI.pm. It is done in this way.
# Problematic Upload Entry
strong('Choose File to upload: '),
filefield(-name=>'upload',-size=>60),br,
However when I tried to capture the result.
Although it can identify the file name, but it
cannot identify it's value.
my $file = param('upload');
# This line print successfully
print h2('File name'), $file;
# But not the subsequent lines
# Line 55 begins here
print h2('File MIME type'), uploadInfo($file)->{'Content-Type'};
while (<$file>) {
print;
$length += length($_);
}
print h2('File length'), $length;
The error message given by the problematic line 55 is:
Can't use an undefined value as a HASH reference at /home/ewijaya/publ
+ic_html/MyTest/cgi-bin/price_debug.cgi line 55.
Furthere more if you comment out the
uploadInfo($file)->{'Content-Type'} line. The content inside while loop doesn't get printed.
To my understanding, I have constructed the uploadInfo() method as according to the doc. But is there anything else that I missed here?
The complete code listing can be found here:
#!/usr/bin/perl -w
use strict;
use CGI qw/:standard :html3/;
use CGI::Carp qw( fatalsToBrowser );
print header, start_html('Order Ice Cream with Price'),
h1('Order Ice Cream with Price');
generate_form();
print_results();
sub generate_form {
print hr, start_form,
strong('Your email: '),
textfield( -name => 'user_email' ), br, br
strong('Cone: '),
radio_group(
-name => 'cone',
-multiple => 1,
-values => [qw/sugar waffle/]
),
br, br
strong('Number of Units: '),
textfield( -name => 'no_unit' ), br, br
# Problematic Upload Entry
strong('Choose File to upload: '),
filefield(-name=>'upload',-size=>60),br,
+
submit( -value => 'Submit' ),
end_form, hr;
}
sub print_results {
print "You ordered ",
param('no_unit'), ' unit of ', param('cone'), ' cone.';
print br;
my $ct = param('cone');
my $nu = param('no_unit');
my $uemail = param('user_email');
my $length;
my $file = param('upload');
# This line print successfully
print h2('File name'), $file;
# But not the subsequent lines
print h2('File MIME type'), uploadInfo($file)->{'Content-Type'};
while (<$file>) {
print;
$length += length($_);
}
print h2('File length'), $length;
print br;
}
And it can be tested through this
link.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.