|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Perl Beginner - I need basic help
Hi,
I’m a very new Perl user. How can I insert CGI scripts into Perl and test them? I have Perl installed and I have Apache 1.3.19 running in a console window (Windows 98) on my computer. I have no idea where to start. I’ve looked through the Perl .html help docs, but I can’t find any info. for beginners, like myself, who know very little about Perl. Thanks, Jenny |
|
#2
|
|||
|
|||
|
Just run your script though perl with this command:
perl -c script.pl (where script.pl is the name of your CGI script) the -c option will tell perl just to check your syntax |
|
#3
|
|||
|
|||
|
When running your CGI from the command line, you can pass parameters to it seperated by newlines, and then CTRL-D to process the script. This is much more useful than staring at a 500 error in your browser.
Dave |
|
#4
|
|||
|
|||
|
Hiee,
When I type... perl -c script.pl nothing happens. After I press enter I'm just brought to the next line - waiting. When I play with code in Perl I'm suppose to go to double click on perl.exe right? meatlog, I'm not clear as to what you wrote. Can you explain a tincy more? Thanks guys, Jenny |
|
#5
|
|||
|
|||
|
Jenny, you shouldn't need to doubleclick perl.exe, it should be in your path (is that right term on windows?). I have to admit my unfamiliarity with ActiveState perl here. My comments were focused on testing a CGI from the command line as opposed to when you view it in a browser. Since it seems you are very beginner, have you considered a good starting perl book such as Learning Perl?
URL 3rd edition due out in July, but I wouldn't wait, all the core concepts are delivered lucidly in this edition. Dave |
|
#6
|
|||
|
|||
|
Several things to check
Jenny,
There are several things to check...first, when you installed active state, it should have put an entry into your path, so type the following on the command line to verify that: echo %PATH% I installed perl in c:\perl, so my path statement looks like this: C:\perl\bin;c:\windows;c:\windows\command that will ensure that when you type perl -c foo.pl that c:\perl\bin\perl.exe -c foo.pl is executed. The later distributions of activestate does not associate .pl files to the perl application. You will have to do that. Just look up associate under the help menu and it will walk you thru it. ############################################### TO BE ABLE TO RUN PERL SCRIPTS USING WEB SERVER If you have a web server (I think you said apache) that will execute cgi scripts (apache does) you will have to make sure that if you are on a windows platform that the are .pl and NOT .cgi. Also, you will have to edit the httpd.conf file and make sure that ScriptAlias and AddHandler has values similar to this: AddHandler gi-wrapper .pl ScriptAlias /cgi-bin/ c:\inet\pub\www\cgi-bin or what ever the path is to your web server. |
|
#7
|
|||
|
|||
|
Success!
Here’s the script that worked for me... #!c:/perl/bin/bin/perl print "Content-type: text/html\n\n"; print "Not Hello, again!\n"; My main and most recent problem was my incorrect path in my shebang line, but it’s ok now. Thanks. Does my shebang line need to be like that for every script I have? Will my shebang line be different if I'm posting a .cgi script to a webhost’s server? I have a few more important questions… The script above was named simple.cgi When I tested my script I was successful using the following url in both Netscape and IE… http://localhost/cgi-bin/simple.cgi However, my test only worked for Netscape under this url… http://127.0.0.1/cgi-bin/simple.cgi When I entered the above url in IE I received this message... Connection refused Description: Connection refused Is this a problem? Why did this happen only for the http://127.0.0.1/cgi-bin/simple.cgi address on Internet Explorer? What the difference between the localhost and the 127.0.0.1 extension? And finally… What do I open when I want to test scripts directly in perl? Do I open (double click) perl.exe ? What would be the point of testing scripts directly in perl when I can test them in a browser? You write about Active state. What is that? As far as I know I just have Perl. How can I get to this command line you keep referring to? What file do I open to get to it? Thanks so much for taking the time, I really appreciate it. Jenny |
|
#8
|
|||
|
|||
|
ok...let's clear up some confusion on both sides. I'm unsure about some of the things you are saying, so let's work through this....
<<<question>>> My main and most recent problem was my incorrect path in my shebang line, but it’s ok now. Thanks. Does my shebang line need to be like that for every script I have? Will my shebang line be different if I'm posting a .cgi script to a webhost’s server? <<<answer>>> the shebang will most likely change if the script is moved to a webhost server. most web hosts run a linux or some flavor of unix and the shebang would probably be #!/usr/bin/perl or #/usr/local/bin/perl. I am actually surprised, I didn't realize apache web server would run .cgi files (scripts) on a windows platform. I thought it would require the extension to be .pl......I stand corrected. If I had to guess thats the only reason the shebang was required in your script. If you had named your script simple.pl and c:\perl\bin\ was in your path, then the script would run without a shebang at all. try it and see. <<<question>>> When I entered the above url in IE I received this message... Connection refused Description: Connection refused Is this a problem? Why did this happen only for the http://127.0.0.1/cgi-bin/simple.cgi address on Internet Explorer? What the difference between the localhost and the 127.0.0.1 extension? <<<answer>>> are you sure you didn't have a type...there shouldn't be difference between the two. localhost is 127.0.0.1. I don't think it's a problem...If nothing else, (long shot here), try updating internet explorer from microsoft updates and see if the problem persists. <<<question>>> What do I open when I want to test scripts directly in perl? Do I open (double click) perl.exe ? What would be the point of testing scripts directly in perl when I can test them in a browser? <<<answer>>> you can check the sytax for the script by using perl -c simple.cgi or perl -c simple.pl... As for as testing the values and such for cgi scripts, just put them in there place on the web server and run them....trial and error. I usually check the httpd log file for errors if it bombs out on me. don't double click on a perl file in windows...such ans simple.pl....it will run in a command window and go away...useless. <<<question>>> You write about Active state. What is that? As far as I know I just have Perl. How can I get to this command line you keep referring to? What file do I open to get to it? <<<answer>>> Activestate is perl for windows...type perl -v at the command prompt (in windows click on start, click on run and enter cmd. a dos widow will come up...that's the command line). perl -v will tell you the version and who it is by...I'd like to know, just curious. Well...I hope I've helped ya...hang in there. perl is a lot of fun. It kinda grows on ya. Good Luck! |
|
#9
|
|||
|
|||
|
Quote:
No don't literally type "perl -c script.pl" replace script.pl with the name of your script so if your script is counter.pl type this: "perl -c counter.pl" etc. |
|
#10
|
|||
|
|||
|
windows shebang:
usually #!C:/Perl/bin/perl -or- #!C:/Perl/bin/perl.exe Use the path to perl.exe |
|
#11
|
|||
|
|||
|
Hi,
Thanks for the info. Jessman72, JeffCT and frozened! Jessman72, my version of perl is 5.004_02. I’m now able to test very basic .cgi scripts (e.g. hello world) with ease. My new problem is getting my .cgi scripts to interact with <html> Forms. So far I’ve been unsuccessful. (Just so you know, I’m testing my CGI scripts with Apache 1.3.19 on Windows 98. I also have Perl on my system). 1. Here’s the html Form I used… <HTML> <HEAD><TITLE>Most Basic CGI Input Page</TITLE></HEAD> <BODY> <FORM ACTION=" c:/program files/apache/apache group/cgi-bin/emailtest.cgi " METHOD="POST"> <P>email, please: <INPUT TYPE="TEXT" WIDTH="25" NAME="Email_Address"> </P></FORM></BODY></HTML> I think part of the problem may lie in the following line… <FORM ACTION=" c:/program files/apache/apache group/cgi-bin/emailtest.cgi " METHOD="POST"> I have spaces in between the words program files and apache group. Could this be part of the reason that my scripts aren’t running? 2. Here’s the .cgi code I used… #!c:/perl/bin/bin/perl use CGI; $cgi = new CGI; $this_prog = $cgi->self_url; print $cgi->header,$cgi->start_html(-title=>"A Simple CGI Input Page"); my $email = $cgi->param('email'); if ($email) { # check to see if it looks like an email. unless ($email =~ /.*@.*\.\w+/) { &killme("Not a valid email address"); } print "<P>Email is $email</P>\n"; print $q->end_html; } else { # no email submitted, we must need to send the input page. print $cgi->center, $cgi->h3($header), $cgi->start_form(-method=>'POST', -action=>"$this_prog"), "Your email address: ", $cgi->textfield(-name=>'email', -size=>'25', -maxlength=>'40'), ' ',$cgi->submit(-name=>'Submit', -value=>'submit'), $cgi->endform,"</center>"; } sub killme { print "<P>$_[0]</P>"; print $q->end_html; die; } I’m lost. Isn’t this Form suppose to email me the email address entered in the Form? Where do I specify my email address? Thanks for reading, Jenny |
|
#12
|
|||
|
|||
|
<FORM ACTION=" c:/program files/apache/apache group/cgi-bin/emailtest.cgi " METHOD="POST">
to: <form action="/cgi-bin/emailtest.cgi" method="POST"> |
|
#13
|
|||
|
|||
|
Hi!
First, please disregard the .cgi script posted above . I couldn’t get that one working so I used another one. I’m trying to test .cgi scripts, but I’m having some trouble. 1. Here’s my html Form… <HTML> <HEAD><TITLE>Most Basic CGI Input Page</TITLE></HEAD> <BODY> <FORM ACTION="emailtest.cgi" METHOD="POST"> <P>email, please: <INPUT TYPE="TEXT" WIDTH="25" NAME="Email_Address"> </P></FORM></BODY></HTML> Frozened, you said I should use this line… <form action="/cgi-bin/emailtest.cgi" method="POST"> …but it didn’t work. So, I used the following… <FORM ACTION="emailtest.cgi" METHOD="POST"> Why did this work and not what you wrote? 2. Anyway, I tested my <html> Form with the following .cgi code. My results were strange. (The .cgi script below is suppose to accept any email address and display it on a new page). #!/usr/local/bin/perl # %FORM = &cgidecode(); $email = $FORM{Email_Address}; print "Content-type: text/html\n\n"; print "<HTML><HEAD><TITLE>Catching an email</TITLE></HEAD>\n"; print "<BODY><P>Address is: $email.</P></BODY></HTML>"; sub cgidecode { local(%vars, $val, $key, $last, $buffer, $pair, @pairs); # Checking the form method (GET or POST) used # in the HTML code. POST method sends data to # standard input, but GET adds it to the URL # and stores it in QUERY_STRING. if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Splitting up the data fields and store in array @pairs, # they are seperated with & @pairs = split(/&/, $buffer); # Splitting the variable names and the values and storing # them in the assoc. array %vars foreach $pair (@pairs) { ($key, $val) = split(/=/, $pair); $val =~ s/\+/ /g; $val =~ s/%(..)/pack("C",hex($1))/eg; if ($key eq $last) {$vars{$key} .= " ".$val; } else { $vars{$key} = $val; } $last = $key; } return(%vars); } After I entered an email address I was brought to a new screen, which displayed the following… #!c:/perl/bin/bin/perl # %FORM = &cgidecode(); $email = $FORM{Email_Address}; print "Content-type: text/html\n\n"; print "\n"; print " Address is: $email. "; sub cgidecode { local(%vars, $val, $key, $last, $buffer, $pair, @pairs); # Checking the form method (GET or POST) used # in the HTML code. POST method sends data to # standard input, but GET adds it to the URL # and stores it in QUERY_STRING. if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Splitting up the data fields and store in array @pairs, # they are seperated with & @pairs = split(/&/, $buffer); # Splitting the variable names and the values and storing # them in the assoc. array %vars foreach $pair (@pairs) { ($key, $val) = split(/=/, $pair); $val =~ s/\+/ /g; $val =~ s/%(..)/pack("C",hex($1))/eg; if ($key eq $last) {$vars{$key} .= " ".$val; } else { $vars{$key} = $val; } $last = $key; } return(%vars); } Does anyone know what could have happened here? Thanks, Jenny |
|
#14
|
|||
|
|||
|
Oopsies!
My shebang line is #!c:/perl/bin/bin/perl Not... #!/usr/local/bin/perl Thanks, Jenny |
|
#15
|
|||
|
|||
|
JennyW, my best advice would be to not use Perl. Learn PHP, it is much easier than Perl. With PHP, all you would have to do is set the action in the form to "recieve.php" and put this code in recieve.php:
PHP Code:
To download PHP for apache, get it here http://www.php.net
__________________
z-lite was here |