http://qs321.pair.com?node_id=550719

monkfan has asked for the wisdom of the Perl Monks concerning the following question:

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.

Regards,
Edward