Hack 7. Install Perl Modules
A number of hacks in this book use Perl scripts. This hack covers installing Perl, Perl modules, and Perl support in your web server.
Perl is a widely used scripting language that I use in this book to perform a variety of podcast-related tasks. Perl comes preinstalled on Mac OS X and Linux, and it's easy to install on Windows.
If you have the time, it's worth learning Perl. The language syntax can be a little confusing at first but becomes familiar quickly. A lot of the value of Perl comes from the CPAN library of modules (http://cpan.org/), which covers every conceivable need.
1.8.1. Perl Modules for Podcasting
Here are some helpful Perl modules that should interest podcasters:
1.8.2. Installing Perl Modules on Linux and Mac OS X
One of the best Perl modules is the CPAN module, which automatically downloads Perl modules and installs them. Even better, if the module you want depends on other modules, it will download those modules first and install them before installing the module you requested.
To run the CPAN module, invoke this command:
% sudo cpan
sudowill run the script as root so that you can install modules in the main Perl library directory. The shell portion of the command line instructs the CPAN module to go into interactive mode. From here, you can type in install commands to download and install modules.
The first time you run the CPAN module it will ask you a lot of questions about where it should look for modules. Make sure to specify a few sites so that if one or two go down you won't have any problems downloading and installing modules.
Once the setup is done the CPAN module will download the tarball of the module you requested and install it for you.
If you want to forego the interactive shell, you can request a specific module right from the command line, with this syntax:
% sudo cpan install XML::Simple
184.108.40.206 Installing web Perl scripts on Mac OS X.
Apache comes preinstalled on Mac OS X. You will need to enable it by using the Personal Web Sharing option in the Sharing section of System Preferences.
Once Apache is active, you can copy your Perl script to the/Library/ WebServer/CGI-Executables/directory. Make sure the script is executable by changing its Unix permissions. The following shell commands copy a file callednetwork.plto the CGI directory and change its permissions to enable execution:
% cp network.pl /Library/WebServer/CGI-Executables/ % chmod 755 /Library/WebServer/CGI-Executables/network.pl
With this done, you should be able to use Safari to surf to the page on your local web server (http://localhost/cgi-bin/network.pl) and see the output of the script.
If you have trouble, you can try using this very simple script as a starting point:
#!/usr/bin/perl print "Content-type: text/html\r\n\r\n"; print "<html><body>Hello!</body></html>";
The output is a web page that says "Hello."
220.127.116.11 Installing Perl scripts on your server.
Installing Perl scripts on a Linux-, Unix-, or Mac OS X-powered web server is similar to installing them on Mac OS X. You copy the script into yourcgi-bindirectory, and change its permissions to 755 to make it executable. Unfortunately, the name and location of yourcgi-bindirectory changes with each ISP.
The good news is that installing Perl scripts is a common issue for ISPs, so they will have documentation on how to install them in their Frequently Asked Questions (FAQ) document, or in their knowledge base.
1.8.3. Installing ActiveState Perl on Windows
Windows doesn't come preinstalled with Perl. Can you believe it? Crazy. So, to run Perl scripts on Windows, you will need to install Perl first. The most direct way to do that is to install ActivePerl (http://activestate.com/). Use all the default options during the installation and you should have Perl installed inc:\perl.
Open a new command prompt so that you pick up the latest PATH setting, and run this command to test the Perl installation:
C:\> perl v
If all is well, you will get a message that gives you the version of the Perl interpreter, some licensing information, and so on.
Another Perl installation option is to use Cygwin (http://cygwin.com/). Cygwin is a complete Unix environment for Windows that includes Perl among the available packages. You can use ActiveState or Cygwin Perl to run the scripts in this book, but ActivePerl integrates nicely with Internet Information Services (IIS). So, I recommend installing ActivePerl to use the scripts in this book. Thankfully the two PerlsActivePerl and Cygwin Perldo not conflict with one another if both are installed on the same machine.
18.104.22.168 Installing Perl modules on Windows.
The ActiveState alternative to the CPAN module is a utility namedppm(Programmer's Package Manager).ppmcan install any module in its repository. And there are a lot of modules in the repository! To see the complete list, go to the ppm site (http://ppm.activestate.com/).
To start an interactive session with ppm, type this at the command prompt:
From here, you can use install commands to install new modules:
ppm> install Mail-Box
This will install theMail::BoxPerl module. As with CPAN, the package manager will download any modules required to run the one you want to install, before installing yours.
One downside withppmis that you can't specify the module name directly. Instead ofMail::Boxyou have to use the package filename,Mail-Box. Usually the translation is a simple conversion of the :: to -. However, you might find that you need to search for the package name that corresponds to the module. To search, use this syntax:
ppm> search XML::RSS
You will get back a list of qualifying packages.
22.214.171.124 Installing Perl scripts in IIS.
Windows XP and earlier editions allow the installation of a cut-down version of the IIS web server. To install it, use the Add or Remove Programs control panel, and then select Add/Remove Windows Components and the Internet Information Services.
Once IIS is installed, you can use the Internet Information Services manager in the Administrative Tools portion of the control panel to configure the IIS server. The first step is to create a new virtual directory that will host the Perl scripts. Navigate down into the Web Sites section and into the Default Web Site item. Now, right-click the righthand portion of the window and select Virtual Directory… from the New submenu.
The first step in the Virtual Directory Creation Wizard is to give the new directory a name. I called minescripts. This is shown in Figure 1-15.
Figure 1-15. Naming an IIS virtual directory
Then you have to assign it to a real directory on your hard drive that will host the scripts. I usedc:\scripts, as shown in Figure 1-16.
This means that any requests to http://localhost/scripts/ will invoke the corresponding Perl script in the c:\scriptsdirectory. So,http://localhost/scripts/myscript.pl becomes c:\scripts\myscript.pl.
Figure 1-16. Assigning the virtual directory to a real directory
The last step is to set up the permissions on the virtual directory. I chose just to allow for execution of the scripts, by choosing the options shown in Figure 1-17.
Figure 1-17. Setting the permissions on the directory
Once you have completed the wizard there is one last step. You need to tell IIS that scripts with the.plextension should be run with ActiveState Perl. The first step is to right-click the newscriptsvirtual directory and select Properties. From here, click the Configuration… button.
This brings up the set of application mappings that specify which extensions go to which interpreter. Click Add to create a new mapping and to specify the mapping, as shown in Figure 1-18.
Click OK on the dialog in Figure 1-18, and then OK again on the dialogs that follow, until you are back at the IIS manager. Now you can add Perl scripts into the c:\scripts\ directory and access them through your browser.
Use this simple test script to make sure everything is working properly:
print "Content-type: text/html\n\n"; print "<html><body>Hello!</body></html>";
Figure 1-18. Assigning Perl to the .pl extension
Save this file asc:\scripts\test.pl.Then use your web browser (hopefully the new Firefox browser, downloadable at http://firefox.com/) to surf to http://localhost/scripts/test.pl. If the web page shows the word Hello, you are cooking with gas.
126.96.36.199 Installing Perl scripts in Apache on Windows.
Apache is an open source web server that provides an excellent alternative to IIS on Windows. Download the Windows installer for Apache (http://apache.org/). For the hacks in this book, I recommend using the 1.3 version of the server. Follow the installer and select that you want to run it as a service.
Next, create a script in the c:\Program Files\Apache Group\Apache\cgi-bindirectory calledtest.pl, with the following contents:
#!c:\perl\bin\perl.exe print "Content-type: text/html\n\n"; print "<html><body>Hello!</body></html>";
The trick here is to get the location of the Perl interpreter right. I used the default location for the ActiveState Perl interpreter. The location of your interpreter will vary if you installed it somewhere else.
After you have saved the test script in the cgi-bin directory, point your browser to http://localhost/cgi-bin/test.pl and you should see the word Hello.
How cool is that? Instant web serving with no muss or fuss.