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

In reply to Problematic UploadFile() method in CGI.pm by monkfan

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.