View Full Version : perl.pl?method=get/post
DELOCH
08-04-2006, 12:26 AM
I have a little question...
how do I do it so my perl:
http://localhost/cgi-bin/perl.pl?name=Dima
allows my perlscript to print $_GET['text'] or $_POST['text']...
is it possible with a .pl script or only a .cgi can
If it can, how?
Thanks a lot :D
FishMonger
08-04-2006, 01:53 AM
$_GET['text'] or $_POST['text'] is php syntax.
Perl's CGI module makes it very easy because it handles both and allows you to use the same line of code no matter if it was submitted via GET ot POST.
use CGI;
$name = param('name');
http://search.cpan.org/~lds/CGI.pm-3.20/CGI.pm
DELOCH
08-04-2006, 02:04 AM
uhh...
Unidentified subroutine &main::param called at perl.pl line 4
I never had this error in PHP...
FishMonger
08-04-2006, 02:06 AM
Oop, my error. I normally use the OO methods instead of the procedural.
use CGI ':standard';
$name = param('name');
or
use CGI;
$cgi = new CGI;
$name = $cgi->param('name');
or
use CGI;
$cgi = new CGI;
%form = $cgi->Vars;
all form fields have now been imported and assigned to the %form hash
to access the 'name' field
$form{'name'}
DELOCH
08-04-2006, 02:21 AM
when I add ?name=myname
it gives an error 500 on me...
what is wrong?
FishMonger
08-04-2006, 02:30 AM
I don't know...you haven't provided enough info for us to troubleshoot.
Please post your script and some details on how you're executing/testing it.
DELOCH
08-04-2006, 02:50 AM
before edit: http://localhost/cgi-bin/perl.pl
after edit: http://localhost/cgi-bin/perl.pl?name=myname
on edit: error page, before: blank page...
yeah...
FishMonger
08-04-2006, 02:58 AM
A 500 error is often caused because the web server can't execute the script or because it's trying to print something before it prints the html headers.
Verify the file permissions and that it's in a directory that is configured to run scripts (default script dir is cgi-bin).
Please post the contents of the script so I can see if there are any obvious issues.
FishMonger
08-04-2006, 03:11 AM
Look at the web server error log and post the error related to your script.
DELOCH
08-04-2006, 08:07 PM
sorry I reinstalled XAMPP... I don't have the script anymore, although it was something like
#!/usr/bin/perl -w
use strict;
my($x) = << "EOF"
XHTML beginning: xml + doctype + <html>
EOF
print "<head>\n";
print "</head>\n";
print "<body>\n";
#############################your script... ############
print "</body>\n";
print "</html>\n";
FishMonger
08-04-2006, 09:59 PM
There are several reasons why your script failed, but instead of going over those details, I'll post a short test script for you to try. In a prior question you asked how to change the DOCTYPE to Transitional. If you use the CGI module to create the headers, Transitional is the default doctype. This test script shows how to set it to Strict.
#!c:/perl/bin/perl
use strict;
use CGI ':standard';
print header(),
start_html(-dtd=>['-//W3C//DTD HTML 4.01 Strict//EN',
'http://www.w3.org/TR/html4/xhtml1-strict.dtd' ]);
my $name = param('name');
if ($name) {
print h2("Hello $name, how are you today?");
}
else {
print h2("Hello, What is your name? "),
start_form(),
textfield('name', '', 20,20),
submit(),
end_form;
}
print end_html();
You should read the documentation for the CGI module. The link is in my first post.
DELOCH
08-05-2006, 03:13 AM
I requote:
Server error!
The server encountered an internal error and was unable to complete your request.
Error message:
Premature end of script headers: PERL.cgi
FishMonger
08-05-2006, 04:30 AM
The script I posted works on my system, so there must be a config problem on yours.
Are you on Windows or Linux?
You need to make sure the shebang line (the first line at the top of the script) points to where perl is located.
The file permissions need to be set correctly (chmod 755).
The web server is configured to execute the script and that the script is in the proper directory (normally the default location for scripts is the cgi-bin directory).
Check your web server error log and post the exact error message. The error message in the log file will narrow down the problem. Without knowning the exact error message from the log file, everything is just guess work.
You can add this line to the script (just after the use CGI line) to get a more complete error message sent to the browser.use CGI::Carp qw(fatalsToBrowser);
DELOCH
08-05-2006, 07:22 PM
I use Windows XP at this moment...
FishMonger
08-05-2006, 09:13 PM
If you can't provide the details of your setup and the exact error message, I won't be able to help you.
DELOCH
08-06-2006, 06:08 PM
that quote was the exact error message :/
KevinADC
08-06-2006, 06:27 PM
sounds like the shebang line could be wrong:
#!c:/perl/bin/perl
FishMonger
08-06-2006, 07:09 PM
It's possible that the path in the shebang line is wrong for his system, but if so, then I'd expect this type of error message in the log instead of the "Premature end of script headers: PERL.cgi".
[Sun Aug 06 10:59:44 2006] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified. : couldn't create child process: 720002: deloch.pl
[Sun Aug 06 10:59:44 2006] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified. : couldn't spawn child process: C:/Apache2/cgi-bin/deloch.pl
If the shebang line is correct, and there is an error elsewhere, such as typo in the calling of the CGI module, then the error message it generates would cause the "Premature end of script headers".
In this case, I changed the use statement to this:
use cGI ':standard';
[Sun Aug 06 11:01:01 2006] [error] [client 127.0.0.1] Premature end of script headers: deloch.pl
[Sun Aug 06 11:01:01 2006] [error] [client 127.0.0.1] Bareword "end_form" not allowed while "strict subs" in use at C:/Apache2/cgi-bin/deloch.pl line 15.
[Sun Aug 06 11:01:01 2006] [error] [client 127.0.0.1] Execution of C:/Apache2/cgi-bin/deloch.pl aborted due to compilation errors.
FishMonger
08-06-2006, 07:26 PM
If we fix the "bareword" issue on the end_form line, then the error would be:
[Sun Aug 06 11:17:56 2006] [error] [client 127.0.0.1] [Sun Aug 6 18:17:56 2006] deloch.pl: Undefined subroutine &main::header called at C:/Apache2/cgi-bin/deloch.pl line 7.
That gives an indication that the CGI module didn't get loaded either due to the typo or because it may not be installed or at least can't be found within the @INC directories.
KevinADC
08-06-2006, 07:55 PM
It's possible that the path in the shebang line is wrong for his system, but if so, then I'd expect this type of error message in the log instead of the "Premature end of script headers: PERL.cgi"........
yes, but since he says the only message was "Premature end of script headers" with no other error, I am confused. I would also expect something like this:
[error] [client 127.0.0.1] (OS 2)The system cannot find the file specified.
if the shebang line was wrong, but I would expect some other error message if the code was wrong in some way. Maybe there is another error in the log the OP missed seeing. I recommend he deletes the error log and retries so he can see a fresh set of server error log messages.
DELOCH
08-07-2006, 01:02 AM
no, my xampp is installed under D:\
and my perl is in c:\usr\bin\
so i use #!c:/usr/bin/perl -w on xampp and #!/usr/bin/perl -w on c:/ using as a program
KevinADC
08-07-2006, 04:39 AM
so is it working now?
DELOCH
08-07-2006, 09:41 PM
no i said everything was perfect, it doesnt work
FishMonger
08-07-2006, 09:54 PM
no i said everything was perfect, it doesnt work
Since it isn't working, everything CAN'T be perfect.
Without more detailed info from you, Kevin and I can't help...we can only guess what kind problem you have with your config and I'm too busy to make these guesses.
KevinADC
08-07-2006, 10:01 PM
Try putting all your programs on the same drive and see if that helps.
DELOCH
08-08-2006, 02:02 AM
I can't, my xampp is on D and my PERL is on C, I put XAMPP on D to save disk space
800megs left...
FishMonger
08-08-2006, 02:33 AM
Do you get any errors with this basic "Hello World" script? If so, copy/post the last 10 or 15 lines from your apache error log.
#!c:/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello World";
If no errors, then run the other sample script I posted, but make sure you change the shebang line to match your environment, then post the last 10 or 15 lines of your error log.
If the "Hello World" script works but the other one doesn't, then you probably don't have the CGI module installed. I've never used XAMPP, but willing to bet that it's missing a number of basic modules that come with almost all other Perl distros which is the price you pay for these "all-in-one" soultions..
DELOCH
08-08-2006, 02:47 AM
nope works like the one I just made
#!/usr/bin/perl -w
print "Content-Type: text/html\n\n";
print "<html>\n";
print " <head>\n";
print " <title>.:: PERL ::.</title>\n";
print " </head>\n";
print " <body>\n";
print " </body>\n";
print "</html>\n";
Works just like plain HTML :D
I think my CGI does not work...
FishMonger
08-08-2006, 03:53 AM
You could just install the CGI module, but who knows how many other basic modules XAMPP left out. My recommendation is to install Activestate perl which not only comes with numerous modules beyond Perl's core modules, but it also has the ppm utility which makes it very easy to install additional modules.
http://activestate.com/Products/ActivePerl/?mp=1
DELOCH
08-08-2006, 03:48 PM
I got the CGI modules, (Mod PERL for XAMPP addon, I will try now)
Wow, it works :D
Thanks guys, you helped a lot :D
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.