Using CPAN to Extend Perl on Mac OS X
So you read Getting Started with Perl on Mac OS X and you're starting to write little Perl scripts to get things done. You learned a few things along the way, and even though you've surely hit some speed bumps, you're still rearing to go. Today, let's talk about extending your abilities by installing some modules from CPAN.
What is CPAN?
CPAN is the "Comprehensive Perl Archive Network" - a collection of over one hundred thousand Perl modules ready to be installed and used by you to extend your Perl chops. What is a Perl module, you ask? Well, a Perl Module is pre-written code that you can include in your code to extend its functionality.
Say you wanted to write some code to interact with the music in your iTunes library. You could spend hours and hours writing sub-methods to do every little task to do so if you didn't first look on CPAN. But if you did, then you would find all of these modules that already did the work for you!
Configuring Your Mac to Use CPAN
Before we get into using CPAN to install modules for your own code, let's talk what happens if you find a really cool project on the Internet that uses CPAN modules. To get that code up and running, you'll need to install the modules from CPAN to your computer. So let's configure your Mac to use CPAN before we do anything else.
First, CPAN uses some low-level tools to install modules on your Mac. Therefore, you will need to have either Xcode installed from the Mac App Store or you will need to download the Command Line Tools for Xcode package from Apple's Developer site. (Note that you need a free developer account to download these tools.) Once you have either Xcode or the Developer tools, let's install some modules!
Installing CPAN Modules on Your Mac
Now, open the Terminal application, type "cpan", then hit Return. You should get a nice long paragraph explaining what CPAN is and how to install it. Type "yes", and then hit Return. This will take you through an installation process that that can potentially ask you a whole bunch of questions. Make it easy on yourself and type "yes" for each one. Once you are up and running, let's install an example module.
When you read through the documentation for that cool little Perl script that does that a cool little thing, you will likely see the developer include some sort of "dependencies" list. For example, lots of applications these days use the internet to fetch data, so the programmer who wrote your script may have used a module called "HTTP::Request" in their code (you can read more about HTTP::Request module here). So let's install the "HTTP::Request" module. Open up a Terminal and type:
You will see a whole lot of output on your terminal. Really, LOTS of output. But, at the end, you should have a shiny "Installation Complete" message - awesome! Go ahead and install the dependencies required for that cool Perl script you found on the internet - you should be up and running!
Next time, we are going to put together skills from the first and second installments of "Getting Started with Perl on Mac OS X" to do something REALLY cool - in the meantime, happy coding!
Meet Your Macinstructor
Richard Myers is an amateur programmer from southern New Jersey. His interests include programming, playing ping-pong, and writing about himself in the third person. He is married and has two old dogs. Contact him at email@example.com.