perl cgi script not working


 
Thread Tools Search this Thread
Top Forums Web Development perl cgi script not working
# 1  
Old 12-28-2011
perl cgi script not working

Hello,
Met a problem learning perl cgi script.
Code:
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:

Code:
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
Code:
9 my $contenttype = $q->uploadInfo($filename)->{'Content-Type'};

which actually looks fine. It can't be Line 3 which is a routine perl line:
Code:
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!!
# 2  
Old 12-30-2011
Hi yifangt,

This line may be the problem:

Quote:
Originally Posted by yifangt
Hello,
8 my $filename = $q->param('/home/yifangt/hello.cgi');
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
# 3  
Old 12-30-2011
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.
# 4  
Old 12-30-2011
Quote:
Originally Posted by yifangt
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
# 5  
Old 01-03-2012
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
# 6  
Old 01-03-2012
Quote:
Originally Posted by yifangt
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:
Code:
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

Moderator's Comments:
Mod Comment Please use next time
code tags

Last edited by vbe; 01-04-2012 at 12:43 PM.. Reason: I just saw the error log and this is running on apache2 and inside cgi-bin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not able to copy the file in perl cgi script

Hello experts, I am facing an very typical problem and hope the issue can be solved. I have a page download.cgi in /cgi-bin folder. use CGI; use CGI::Carp qw ( fatalsToBrowser ); use File::Copy copy("C:\\Program Files\\Apache Software... (8 Replies)
Discussion started by: scriptscript
8 Replies

2. Shell Programming and Scripting

Perl CGI : unable to download the excel sheet from perl cgi page

Hi All, I have written an cgi perl script that displays an image(Excel image) and when clicked on that Image I need to download a excel sheet. I made sure that excel sheet exists in the folder with the given name but still I am not able to download the sheet. print "<center><table... (2 Replies)
Discussion started by: scriptscript
2 Replies

3. Shell Programming and Scripting

Perl cgi pages out of cgi-bin folder in WINDOWS

Hi team, I have a typical problem with cgi pages in apache webserver in WINDOWS I am able to execute(display) the pages that are saved in cgi-bin folder. But I am not able to execute the pages stored in htdocs or other folder other than cgi-bin folder. Could anyone please let me know how... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

Perl : embedding java script with cgi perl script

Hi All, I am aware that html tags can be embedded in cgi script as below.. In the same way is it possible to embed the below javascript in perl cgi script ?? print("<form action="action.htm" method="post" onSubmit="return submitForm(this.Submitbutton)">"); print("<input type = "text"... (1 Reply)
Discussion started by: scriptscript
1 Replies

5. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

6. Web Development

problem with exporting vairable from one perl cgi to another perl cgi script while redirecting.

Can anyone tell me how to export a variable from one perl CGI script to another perl cgi script when using a redirect. Upon running the login.pl the user is prompted to enter user name and password. Upon entering the correct credentials (admin/admin) the user is redirected to welcome page. My... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

7. Shell Programming and Scripting

Perl CGI. no output until backend script is done

It is a basic Perl CGI question, I want to print out "Processing ... " while backend script /script/wait.pl is still running. But acctually, nothing appeared in browser untill /script/wait.pl finished. print "Content-type:text/html\r\n\r\n"; print '<html>'; print '<head>'; print... (4 Replies)
Discussion started by: honglus
4 Replies

8. Shell Programming and Scripting

simple cgi script not working

hi all, i have installed simple cgi-script under apache/cgi-bin directory hello.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print <<END_HTML; <html> <head></head> <body>Hello, World!</body> </html> END_HTML when i hit the url... (6 Replies)
Discussion started by: raghur77
6 Replies

9. Web Development

in cgi perl script a form

hi,i hav a form in cgi perl script.this script accepts a value from user from another html form, and depending upon this value,i need to disable /enable radio buttons in cgi-perl script wen second page is displayed on executing cgi perl script.how do i do it using javascript? (0 Replies)
Discussion started by: raksha.s
0 Replies

10. Shell Programming and Scripting

call shell script from perl cgi script problem

hi,, i have perl scipt with line : system('./try.sh $t $d $m'); in shell scipt try.sh i have the line: echo $1 its not printing value of $t that i hav passed..y is it so..i am running it from apache web server (2 Replies)
Discussion started by: raksha.s
2 Replies
Login or Register to Ask a Question