[sf-perl] perl on a mac

Lara Ortiz de Montellano lara.ortiz.de.montellano at comcast.net
Mon Nov 17 14:21:27 PST 2008

1. Re: perl on a mac (Walt Sanders)

>Terminal won't run it of course, it just spits that code back at me.   
>But, if I try to run it in a browser, of try to preview it in an  
>editor, it still spits code back at me.  This is any program that runs  
>perfectly fine when I upload it to any one of my ISP servers.  I can  
>just download any working .cgi or .pl from one of my websites and it  
>won't run on my machine.

To expand a bit on Darin's instructions in a Mac specific context (and 
this is going to be long and possibly something you already know)...

It sounds like you're seeing the correct, but raw HTML when you expect 
to see interpreted HTML if you run the code on the command line, and raw 
perl code if you open it in the browser?

If so, the deal is that the terminal/command line executes the Perl but 
does not render HTML.   This is correct and expected behaviour.   You 
should see the same thing if you run the script on Windows under
            cmd [OK]
            c:> perl c:\some\path\to\your\script.pl

Opening the perl script directly in the browser will get you the raw 
Perl code because the browser itself cannot execute the Perl code, it 
only understands the HTML (and figures anything else is plain text).

So... you need to execute the Perl by having your browser ask a web 
server to execute the script and return the (HTML) contents to the browser.

To do this:

1) Configure your web server to allow CGI scripts:

a) In Terminal, use the whoami command to see what the Mac thinks your 
user id is.

b) Under /etc/httpd/users (under 10.4) or /etc/apache2/users/ (10.5) 
create a file called username.conf where "username" is your userid name 
on the mac.

c) In the /etc/apache2/users/username.conf file, add the text:

<Directory "/Users/username/Sites/">
    AllowOverride All

d) Create the file /Users/username/Sites/.htaccess

set the permissions on the file:

sudo chgrp www .htaccess
chmod 750 .htaccess

and in the file, add the text:

AddHandler cgi-script .pl
Options ExecCGI

e) Under the apple menu, choose System Preferences and click on Sharing, 
then turn off (by unticking) Personal Web Sharing, then turn it back on 
(by ticking Personal Web Sharing).

2) Set your script up to be accessible to the web server

a) In Terminal, run     which perl     to see which Perl you're using, 
and make a note of the path (usually /usr/bin/perl)

b) Add a shebang line to your perl script to tell the web server how to 
execute your script, using the path  you got in 2a:


c) copy or move your file to /Users/username/Sties/scriptname.pl

d) Set the file permissions to allow world execute on the script:

chmod a+x /Users/username/Sites/scriptname.pl

3) View the file in your browser at


Do you see what you were expecting? 

If so, you'll want to read up a bit on Apache  CGI  and you may also 
want to tweak the Apache and script file permissions to make it a little 
more secure.

-Lara O.

More information about the SanFrancisco-pm mailing list