Perl Testing
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hi,
Im learning Perl at uni on windows, but do all my work at home on OSX 10.4. Is there a way to run Perl scripts in a browser like Firefox on OSX?
I run MAMP and thought that was Perl enabled, but its not.
Cheers
Brad
11 replies
Sign in or join Beachball Help to reply.
- 0
etresoft ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Perl is enabled, but Perl is a little more difficult to use. There is no real reason for this, it is just "the way things were done".
You will need to put your Perl scripts into your CGI-bin directory, wherever that happens to be. You could modify your Apache configuration to run Perl from any directory, but that is not commonly done.
Basically, Perl suffers in this aspect just because it was first and that is the way CGIs were done then. PHP came along and the default setting allowed PHP scripts to run from any directory. That is the only difference - the default setting.
Just find your CGI bin directory and put your perl scripts in there and they should work via Apache's cgi-bin alias, whatever that happens to be.
- 0
B Real ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
etresoft wrote:
Perl is enabled, but Perl is a little more difficult to use. There is no real reason for this, it is just "the way things were done".You will need to put your Perl scripts into your CGI-bin directory, wherever that happens to be. You could modify your Apache configuration to run Perl from any directory, but that is not commonly done.
Basically, Perl suffers in this aspect just because it was first and that is the way CGIs were done then. PHP came along and the default setting allowed PHP scripts to run from any directory. That is the only difference - the default setting.
Just find your CGI bin directory and put your perl scripts in there and they should work via Apache's cgi-bin alias, whatever that happens to be.
etresoft wrote:
Perl is enabled, but Perl is a little more difficult to use. There is no real reason for this, it is just "the way things were done".You will need to put your Perl scripts into your CGI-bin directory, wherever that happens to be. You could modify your Apache configuration to run Perl from any directory, but that is not commonly done.
Basically, Perl suffers in this aspect just because it was first and that is the way CGIs were done then. PHP came along and the default setting allowed PHP scripts to run from any directory. That is the only difference - the default setting.
Just find your CGI bin directory and put your perl scripts in there and they should work via Apache's cgi-bin alias, whatever that happens to be.
Wow. A reply! Ive posted this on many forums and no one seems to want to know.
Ok, so running mamp, ive got the following file structure:
/htdocs/registration.html - this file calls the perl script using
<form method = "POST"
action = "http://localhost:8888/cgi-bin/passcheck09.pl">So the .pl script is sitting in the cgi-bin. When I run the script in firefox, I get:
"Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log."The error log tells me:
"[Tue Mar 11 22:49:47 2008] [error] [client 127.0.0.1] Premature end of script headers: passcheck09.pl, referer:
http://localhost:8888/perl/registration.html"So im confused because these exact same scripts run on my university servers no problem, ive just copied them across and altered directories. So I think the problem is in trying to run perl on my mac at home. But would I be right in thinking that the fact that im getting the Premature end of script error means that Perl is running successfully, but its my code thats wrong?
Am I allowed to post code on here if that might help? Sorry if ive got code quotations wrong in this post..
- 0
etresoft ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
B Real wrote:
"[Tue Mar 11 22:49:47 2008] [error] [client 127.0.0.1] Premature end of script headers: passcheck09.pl, referer:
http://localhost:8888/perl/registration.html"There are lots of things that could cause that. I really can't tell from this end. Make sure you can run the scripts using Perl from the command line. There may be some module that you don't have installed on your Mac. If command-line Perl works, then you need to look into why CGI perl isn't working. That is going to be more difficult because it is entirely dependent on how your server is setup.
So im confused because these exact same scripts run on my university servers no problem, ive just copied them across and altered directories. So I think the problem is in trying to run perl on my mac at home. But would I be right in thinking that the fact that im getting the Premature end of script error means that Perl is running successfully, but its my code thats wrong?
If the code runs elsewhere, it is probably fine. Run it and look for module errors. Then look at Apache configurations.
Am I allowed to post code on here if that might help? Sorry if ive got code quotations wrong in this post..
I don't think it will help because I think your code is fine. If you want to, put your code between two lines of:
{ code }
{ code }
without the spaces. Here is an example:
some code - 0
Charles Minow ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
etresoft wrote:
If the code runs elsewhere, it is probably fine. Run it and look for module errors. Then look at Apache configurations.Hmm. Maybe the path to Perl is different on the server? For OS X, you need either of these two:
<pre class="command">#!/usr/bin/perl</pre>
or
<pre class="command">#!/usr/bin/env perl</pre>
The first one will work for sure on your Mac, but might break on your server if it has a different path to Perl. The second one is more portable, though I've never had a need to use it, and may make it harder to use command line switches to control Perl.
charlie
- 0
Charles Minow ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hi Brad--
I just downloaded MAMP to look at it and Perl CGIs should work. Further down, you posted a line from your log, what's the line above it? For example, if I create a script and try to run it, I get this error if it's not executable:
[Tue Mar 11 21:10:28 2008] [error] [client 127.0.0.1] (13)Permission denied: exec of '/Applications/MAMP/cgi-bin/test.pl' failed
[Tue Mar 11 21:10:28 2008] [error] [client 127.0.0.1] Premature end of script headers: test.plMaking the script executable fixes that error.
But if I deliberately make a mistake on the shebang line (i.e.,
#!/user/bin/perl, I get this error:[Tue Mar 11 21:16:38 2008] [error] [client 127.0.0.1] (2)No such file or directory: exec of '/Applications/MAMP/cgi-bin/test.pl' failed
[Tue Mar 11 21:16:38 2008] [error] [client 127.0.0.1] Premature end of script headers: test.plAnother common error is to forget to print the http headers before sending output to the browser. If you do, you will only get the "Premature end of script headers" error, and no other explanation.
charlie
- 0
etresoft ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
There are a whole bunch of things that could cause this error. Running CGI scripts in Apache can get quite complicated. Most people will admit that Perl is a superior language to PHP. But it has become so easy to do things in PHP that Perl isn't as popular anymore.
To debug this problem you need to take things one step at a time. First make sure that your perl scripts can run on their own. Before worrying about shebang lines, just type "perl -wc <name of your cgi script>.pl" to make sure that you have all the modules you need. Then worry about shebang lines. Then look at Apache config settings to get Perl running as a CGI. Then look at getting mod_perl to do it.
It actually isn't that complicated if you go slow and take things one step at a time.
- 0
B Real ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
OK, ive created a simple test file in /volumes/macintosh hd/applications/mamp/cgi-bin/testperl.pl that looks like
#!/usr/local/bin/perl
print "testing Perl";In terminal command line: perl -wc testperl.pl returns:
testperl.pl syntax OKAnd running: perl testperl.pl on the command line also works and returns the print output as expected. So it seems I can run a perl script from my command line.
When I open up Firefox and put this file in the URL :
http://localhost:8888/cgi-bin/testperl.plI get the Internal Server Error, with the last two lines reading:
[Wed Mar 12 16:29:32 2008] [error] [client 127.0.0.1] (2)No such file or directory: exec of '/Applications/MAMP/cgi-bin/testperl.pl' failed
[Wed Mar 12 16:29:32 2008] [error] [client 127.0.0.1] Premature end of script headers: testperl.plSo its not a permissions thing - im reasonably comfortable with file permissions & always check those first anyway. Looks like apache just can't even see the testperl.pl file? Is this something to do with the apache configuration file /applications/mamp/conf/httpd.conf?
- 0
etresoft ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
B Real wrote:
So its not a permissions thing - im reasonably comfortable with file permissions & always check those first anyway. Looks like apache just can't even see the testperl.pl file? Is this something to do with the apache configuration file /applications/mamp/conf/httpd.conf?Perhaps. You should review the documentation for MAMP to figure out how to do this. If it were a default Apple configuration I could check myself but at this point it is a 3rd party problem.
- 0
Charles Minow ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
B Real wrote:
OK, ive created a simple test file in /volumes/macintosh hd/applications/mamp/cgi-bin/testperl.pl that looks like
#!/usr/local/bin/perl
print "testing Perl";Yep, it's the path to your Perl executable. You need to use this on your Mac:
#!/usr/bin/perlBut that probably won't work on the server, so try falling back to this:
#!/usr/bin/env perlWhich might allow you to run the scripts on both machines. Otherwise, you'll need to change the first line when you upload it to the server.
So its not a permissions thing - im reasonably comfortable with file permissions & always check those first anyway. Looks like apache just can't even see the testperl.pl file? Is this something to do with the apache configuration file /applications/mamp/conf/httpd.conf?
No, the error message in this case isn't very clear, but it's saying that it can't find the Perl executable mentioned in the first line, in order to run the script.
You'd actually get an error if you ran your script in Terminal without using
perl, i.e., like this:<pre class="command">./testperl.pl</pre>
You should get something like this:
<pre class="command">-bash: ./testperl.pl: /usr/local/bin/perl: bad interpreter: No such file or directory</pre>
When you run it like this:
perl testperl.pl, you're bypassing the line at the top of the script that tells the shell where to find the interpreter.charlie
Message was edited by: Charles Minow, fix some formatting issues
- 0
B Real ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
YES!!! Im in!! I cannot begin to explain my sense of elation.
A combination of both of your advice got me there in the end:
#!/usr/local/bin/perl -Twwas the wrong shebang, but:
#!/usr/bin/perl -Tw
and
#!/usr/bin/env perl -Twboth work fine now, but only after changing the httpd.conf for my MAMP servers, which I was doing incorrectly yesterday (and all 2 weeks prior to that). I did try the shebangs you suggested yesterday, but didn't work because I hadn't configured the apache file correctly.
So going back to etresofts post:
etresoft wrote:
Before worrying about shebang lines, just type "perl -wc <name of your cgi script>.pl" to make >sure that you have all the modules you need. Then worry about shebang lines. Then look at Apache >config settings to get Perl running as a CGI.this is how I got there. My documentation on how to configure apache for osx was referring to the /etc/httpd/httpd.conf file (which I believe is the OSX apache configuration file - that I cant seem to make writable for some reason) whereas I was using it to configure the MAMP httpd.conf file which is slightly different, tho not by much.
So as i understand it now, I have enabled Perl on MAMPs Apache server, and can test these on a web browser using
http://localhost:8888/cgi-bin/ which is the MAMP Apache port. This is completely separate from Apache that is pre-installed on Macs, which I have left untouched. If I wanted to test it on the Mac webserver, I would go to
http://127.0.0.1/~myusername/cgifilehere.cgi after configuring the httpd.config file in /etc/httpd/httpd.config.I can't thank you guys enough. Apple discussions wins again.
- 0
etresoft ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
B Real wrote:
So going back to etresofts post:etresoft wrote:
Before worrying about shebang lines, just type "perl -wc <name of your cgi script>.pl" to make >sure that you have all the modules you need. Then worry about shebang lines. Then look at Apache >config settings to get Perl running as a CGI.this is how I got there. My documentation on how to configure apache for osx was referring to the /etc/httpd/httpd.conf file (which I believe is the OSX apache configuration file - that I cant seem to make writable for some reason) whereas I was using it to configure the MAMP httpd.conf file which is slightly different, tho not by much.
You have to use "sudo" to give yourself root privileges temporarily. Apache is, how shall I say it, very "configurable". For example, in 10.5, the file is in /etc/apache2/.