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

Hi all,

I'm getting this error when using uploadInfo:

Can't use an undefined value as a HASH reference at D:/site/modules/Subs.pm line 804.

In Subs.pm, I've

use strict; use CGI; my $q = new CGI; sub mimetype { my $file = shift; my $mimetype = $q->uploadInfo($file)->{'Content-Type'}; # line 804 return $mimetype; }
Did I forget to import some functions? Please help and thanks in advance :)

Update: Found a forum whereby someone brought up the same problem at

http://www.cookwood.com/cgi-bin/lcastro/perlbbs.pl?noframes;read=9176

The only solution was to have 'use CGI qw(:standard);'

Update2: Found the solution at:

http://forums.devshed.com/showthread.php?t=132647

That led me to look at my sanitize sub. What happened was that $file was "sanitized" for character checking. I had a line in sanitize() as follows:
$checks{'data'} =~ tr/\0//d;
When I commented out that line, the uploadInfo function works.

Replies are listed 'Best First'.
Re: uploadInfo - can't use undefined value as a HASH reference
by sgifford (Prior) on Apr 20, 2004 at 01:40 UTC
    The best way to find out what's going on, and a good programming practice in general, is to add better error checking:
    use strict; use CGI; my $q = CGI->new() or die "Couldn't create CGI object!\n"; sub mimetype { my $file = shift; my $info = $q->uploadInfo($file) or die "No file '$file' was uploaded!\n"; my $mimetype = $info->{'Content-Type'} # line 804 or die "Uploaded file had no Content-type!\n"; return $mimetype; }
      Thanks, sgifford :)

      I did that. It says 'No file '$file' was uploaded'. But the file does get saved into the appropriate directory. Could it be because I'm calling mimetype() (which is in Subs.pm) from another module (Upload.pm) that doesn't have 'use CGI' i.e. 'use CGI' is only imported in Subs.pm?

Re: uploadInfo - can't use undefined value as a HASH reference
by sgifford (Prior) on Apr 20, 2004 at 03:38 UTC

    That led me to look at my sanitize sub. What happened was that $file was "sanitized" for character checking. I had a line in sanitize() as follows:

    $checks{'data'} =~ tr/\0//d;

    When I commented out that line, the uploadInfo function works.

    Sanitizing your data really is a good idea. You can either pull out the Content-Type before you sanitize the filename, or just reject filenames that you don't like:
    $filename =~ /^(\w[\w.]*)$/ or die "Invalid filename\n"; $filename = $1;

    Also, it's strange that your sanitize function would have made a difference at all, unless there were NUL characters in your filenames.

      Also, it's strange that your sanitize function would have made a difference at all, unless there were NUL characters in your filenames.
      It's indeed strange. But that transliterate line is likely to be the culprit in this particular instance, because uncommenting it with '#" triggers the "Can't use undefined value as A HASH reference..." error while commenting it out allows the code to run.