CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
first perl form, 6hrs no luck yet

 



bbxrider
Novice

Nov 1, 2008, 11:19 PM

Post #1 of 13 (3401 views)
first perl form, 6hrs no luck yet Can't Post

my environment is the latest active perl, on winxp pro, and apache
eventually i'm trying to get an advanced open source script, the perl
piece of uber upload working, but since that does not work and its complex, am trying just
to get an idiots guide form/script working for now

when this gets called from the form: (sample is the form field)

#!g:/user/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
$data = param('sample') || '<i>(No input)</i>';
#$data = 'hard coded';
print "Content-Type: text/html";
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">";
print "<title>Echoing user input</title>";
print "<h1>Echoing user input</h1>";
print "<p>You typed: $data</p>";

i get Undefined subroutine &main::param called at C:/apache2/htdocs/joomla/form2.pl line 4 in the browser
and the same in the apache error log
so, something is wrong with my perl installation and/or how its configured by apache?

but if its

#!g:/user/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
#$data = param('sample') || '<i>(No input)</i>';
$data = 'hard coded';
print "Content-Type: text/html";
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">";
print "<title>Echoing user input</title>";
print "<h1>Echoing user input</h1>";
print "<p>You typed: $data</p>";

i get
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
etc etc in the browser but not even rendered, just the literal text
and
'Premature end of script headers' in the apache error log

so its real confusing what is going wrong, if i have an incorrect perl install/config and/or apache config,
why don't i get some type of perl error in the 2nd case?

i can run some basic hello world perl scripts with all print statements
when i try to run the same hello world script with.. print <<END ..... END

#!g:/user/bin/perl -w
print <<END;
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<title>Hello</title>
<h1>Hello world!!</h1>
END

i get the same unrendered text as above in the browser
and 'Can't find string terminator "END" anywhere before EOF' in the apache error log

hopefully i have missed something very basic here?


KevinR
Veteran


Nov 1, 2008, 11:36 PM

Post #2 of 13 (3400 views)
Re: [bbxrider] first perl form, 6hrs no luck yet [In reply to] Can't Post

if you did a default install of activeperl the shebang line looks wrong:

#!g:/user/bin/perl -w

usually on Windows that would be:

#!c:\perl\bin\perl

(forward or backslashes are OK)

but if you really installed to the g drive change that. You also have not loaded the CGI module which imports the param() function. CGI::Carp does not.

See how this works:


Code
#!c:/perl/bin/perl 
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use warnings;
$data = param('sample') || '<i>(No input)</i>';
#$data = 'hard coded';
print "Content-Type: text/html";
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">";
print "<title>Echoing user input</title>";
print "<h1>Echoing user input</h1>";
print "<p>You typed: $data</p>";


If that works you can then improve the perl and the html code.
-------------------------------------------------


bbxrider
Novice

Nov 2, 2008, 10:10 AM

Post #3 of 13 (3391 views)
Re: [KevinR] first perl form, 6hrs no luck yet [In reply to] Can't Post

hi thanks for the help

i did install on the g: drive, starting to run a little low on c:, perl -v runs from the command line, at least

i added the lines, you suggested, (makes sense from the undefined subroutine error that the script needs a reference

to get the subroutines) but no go yet, i still get the same unrendered 500 error text in the browser,

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p> .................

and i get 'Premature end of script headers: form2.pl.....' in the apache error log.

from reading about this error, other people say it can be hard to diagnose. some posts/articles indicated that the -w and carp should give more helpful error messages, but i only got that when i didn't have the :standard reference.


KevinR
Veteran


Nov 2, 2008, 10:58 AM

Post #4 of 13 (3388 views)
Re: [bbxrider] first perl form, 6hrs no luck yet [In reply to] Can't Post

If you are still getting that apache error it is almost surely the shebang line:

#!c:/perl/bin/perl

if you can use "perl" on the command line to run perl scripts from the 'c' drive then you should be able to run perl via apache the same way so try this:

#!perl

See if that works.
-------------------------------------------------


KevinR
Veteran


Nov 2, 2008, 10:58 AM

Post #5 of 13 (3388 views)
Re: [bbxrider] first perl form, 6hrs no luck yet [In reply to] Can't Post

If you are still getting that apache error it is almost surely the shebang line:

#!c:/perl/bin/perl

if you can use "perl" on the command line to run perl scripts from the 'c' drive then you should be able to run perl via apache the same way so try this:

#!perl

See if that works.
-------------------------------------------------


bbxrider
Novice

Nov 2, 2008, 3:52 PM

Post #6 of 13 (3380 views)
Re: [KevinR] first perl form, 6hrs no luck yet [In reply to] Can't Post

thanks for your ongoing reply

the #!perl did not work, in the error log i got:

OS 2)The system cannot find the file specified. : couldn't create child process: 720002: and : couldn't spawn child process: C:/apache2/htdocs/joomla/form2.pl

so it seems it can find the g: location, but somehow just not everything it needs from there?

again i can't even run this

print <<END;
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<title>Hello</title>
<h1>Hello world!!</h1>
END

for the above i get 'Can't find string terminator "END" anywhere before EOF' in the error log, this is a pretty basic script, right?, so if i can't run this there is very basic fundamental problem with either my perl or my apache running perl,

since this is apache, i think maybe i don't have it setup quite right? if somebody also knows apache here is my configurations as i understand for cgi and running perl. i'm running these scripts out of a virtual host root directory and within the virtual host directive for this domain i have

<Directory "C:/apache2/htdocs/joomla"> (the virtual host DocumentRoot)
Options -Indexes FollowSymLinks +ExecCGI (this should allow scripts to run here as well as the cgi-bin)
AllowOverride None
Order allow,deny
Allow from all
</Directory>

i also have: ScriptAlias /cgi-bin/ "C:/apache2/cgi-bin/" and: AddHandler cgi-script .cgi .pl (i think the most basic cgi directives)

i'm going to try a post at an apache forum to see if somebody there can analyze this as well

in my configuration i'm running a few sites via virtual host with php and mysql, so i can get something right


KevinR
Veteran


Nov 2, 2008, 4:44 PM

Post #7 of 13 (3379 views)
Re: [bbxrider] first perl form, 6hrs no luck yet [In reply to] Can't Post

I don't know if it is an apache congif problem or not. I also recommend you ask on an apache forum.

Good luck,
Kevin
-------------------------------------------------


FishMonger
Veteran / Moderator

Nov 2, 2008, 7:51 PM

Post #8 of 13 (3376 views)
Re: [bbxrider] first perl form, 6hrs no luck yet [In reply to] Can't Post

Part of the problem is that

Code
print "Content-Type: text/html";

should be

Code
print "Content-Type: text/html\n\n";


When running apache on Windows, it would be best to add the following directive to httpd.conf

Code
ScriptInterpreterSource Registry



More specifically, it should be in a directory block like this

Code
<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin"> 
ScriptInterpreterSource Registry
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>

Then, it won't matter what path you specify on the shebang line, because it will search the registry based on the file extension of the script.


KevinR
Veteran


Nov 2, 2008, 9:32 PM

Post #9 of 13 (3373 views)
Re: [FishMonger] first perl form, 6hrs no luck yet [In reply to] Can't Post

oops... can't believe I missed that:

print "Content-Type: text/html";

good catch Fish, sure is better having two heads here answering questions than one, especially when the one is mine. Unsure
-------------------------------------------------


bbxrider
Novice

Nov 2, 2008, 10:28 PM

Post #10 of 13 (3372 views)
Re: [FishMonger] first perl form, 6hrs no luck yet [In reply to] Can't Post

thanks for the thoughtful followup

so good news and bad news, i got another script working, suggested, then modified, from the active perl documentation, i got it working with a shebang line first, then it also worked with out the shebang line using the scriptInterpreterSource directive, so that seems better than the shebang line, in case the perl install ever changes or is in a different location when moving to a different server. in the apache writeup about it, it talked about where and what the registry entry was, but searching my registry i didn't see any entries for perl in the section they talked about, so thats strange. the new working script is:
# previous line added to support Apache 1/2
# please adjust to your own Perl path!
use strict;
#use CGI;
use CGI qw(:standard); (i changed this from above for the param function)

my $page = new CGI;
my $msg = "Hello from ActivePerl CGI!";
# my $data = 'hard coded';
my $data = param('sample') || '<i>(No input)</i>';

# print header and start the markup output
print $page->header( "text/html" ),$page->start_html( $msg );
print $page->h2($msg);
print "<p>You typed: $data</p>";
print $page->end_html;
# end code
the difference that i can see from the scripts i was trying is the: use strict; , the 'my' with variables, and the print $page-> syntax, at some point i would like see which one of those made the difference.

the bad news is all the unknowns from above and i still can't get the original target script, ubr_upload.pl, from the open source uber upload application, working. it is a complex script and application. i don't expect to solve that here, unless somebody has a suggestion for the firefox message it gives me: 'Firefox doesn't know how to open this address, because the protocol (c) isn't associated with any programs'. From what i've read so far the '(c)' indicates a path problem, and i've quadrupled checked all the paths and think they be right.

so thanks for all your help, its been a good education here.


KevinR
Veteran


Nov 3, 2008, 12:07 AM

Post #11 of 13 (3365 views)
Re: [bbxrider] first perl form, 6hrs no luck yet [In reply to] Can't Post

Well, you have switched from the function interface of the CGI module (:standard) to using the OOP interface, so drop the :standard when you load the CGI module, try like this:

use CGI;
-------------------------------------------------


bbxrider
Novice

Nov 3, 2008, 9:22 AM

Post #12 of 13 (3355 views)
Re: [KevinR] first perl form, 6hrs no luck yet [In reply to] Can't Post

well not sure what your getting at. to explain what happened, when i first tested the script above, it had the use CGI; it did not have any of the code to accept input from a form. so i added that code, but then it didn't work because it could not resolve the param function. so i changed the use CGI to the qw(:standard) and that allowed the form processing to work. so at this point my understanding is that use CGI; by itself is ok for at least some scripts, but not ones that process form input data. if the script does process form data, and uses the param function, it needs at least to have the (:standard) version of use cgi.

i know this a simplistic understanding of how perl is working, its just where i'm at right now


KevinR
Veteran


Nov 3, 2008, 9:50 AM

Post #13 of 13 (3352 views)
Re: [bbxrider] first perl form, 6hrs no luck yet [In reply to] Can't Post

When in doubt read the documentation:

http://perldoc.perl.org/CGI.html

You will see there are two interfaces into the CGI module, function and OO (Object Oriented). To use the functional interface you load :standard

CGI qw(:standard);

then you can call functions like you were:

$foo = param('bar');

Then there is the OO interface which you are now using in your latest code:

$page = new CGI;

$page is the object and now you call its methods:

$foo = $page->param('bar');

I suggest you stick with the standard interface for now as its meant to be easier. When you are more familiar with the CGI module and OO a little (maybe you already are) then you can take advantage of the OO interface.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives