perl cgi script not working

Hello,
Met a problem learning perl cgi script.

1 #!/usr/bin/perl -wT 
  2  
  3 use strict; 
  4 use CGI qw(:standard); 
  5  
  6 my $q = new CGI; 
  7  
  8 my $filename = $q->param('/home/yifangt/hello.cgi'); 
  9 my $contenttype = $q->uploadInfo($filename)->{'Content-Type'}; 
 10  
 11 print header; 
 12 print start_html; 
 13 if ($contenttype !~ /^text\/html$/) { 
 14         print "Only HTML is allowed<P>"; 
 15         print end_html; 
 16         exit; 
 17 } else  { 
 18  
 19 print "Type is $contenttype<P>"; 
 20 } 
 21  
 22 print end_html; 
 23 

The error message from my /var/apache2/error.log is:

Use of uninitialized value $filename in scalar dereference at (eval 3) line 3.
Use of uninitialized value in hash element at (eval 3) line 3.
Can't use an undefined value as a HASH reference at /usr/lib/cgi-bin/Listing4-2.cgi line 9.
[Wed Dec 28 11:37:10 2011] [error] [client 127.0.0.1] Premature end of script headers: Listing4-2.cgi

I was wondering why Line 9 was of problem

9 my $contenttype = $q->uploadInfo($filename)->{'Content-Type'}; 

which actually looks fine. It can't be Line 3 which is a routine perl line:

3 use strict;

Have spent some time on this, could not figure out the reason. Appreciate any clues on this from a cgi beginner. Thanks a lot! Happy New Year!!

Hi yifangt,

This line may be the problem:

param is used to capture the parameters passed to a cgi script. For example if your cgi script is called test.cgi,

and you ran this script on your web server as

test.cgi?test=1

then, this variable $q->param('test') would be 1.

Since you probably don't have a parameter named '/home/yifangt/hello.cgi', $filename is an uninitialized value, therefore line 9 is an error

Can't use an undefined value as a HASH reference at /usr/lib/cgi-bin/Listing4-2.cgi line 9.

Hope that helps,

Dave

Thanks Dave!
Actually I copied from the book which was working from the context. I did not catch the whole thing.
Then how to correct this error, which would help me understanding, if possible?
Thanks again.

I'm guessing your script will work if you change this line:

my $filename = $q->param('/home/yifangt/hello.cgi');

into

my $filename = '/home/yifangt/hello.cgi';

But that's not how you want it to work. You want the actual parameter name inside param(). See my last post on how that parameter name is decided.

Hope that works,

Dave

Happy new year!

I changed the line to what you suggested, but it still did not work.
The syntax seems fine to me, even the original version. I must have missed something about MySQL configuration, or basic concepts. Appreciate any advise.
Yifang

What happens if you make a (or move the) file called "hello.cgi" and put it inside /usr/lib/cgi-bin/ and then change the line to:

my $filename = 'hello.cgi';

I don't think this has anything to do with your MySQL config. If this fails, hopefully someone with more knowledge than me can be able to help you out.

Best,

Dave