Perl, as a scripting language, is described as a “glue language”. It hasn’t had the benefit of a solid top-down design like similar languages (Python for example), but has been flexible and modular enough that people have added all kinds of nice features and modules. When I first learned Perl in 2000-2001, I never made good use of Perl modules, which were less integrated then I think, and wrote a lot of code myself.
In hindsight, this isn’t such a great approach. I used to work in a bio-informatics lab where we analyzed DNA sequences for certain dangerous viruses, and I was often tasked with parsing out huge text files and outputting the data into reports. I wrote passable Perl at the time, and was able to get the job done, but Where I work now I work with some very bright people and Perl gurus, and one of the most impressive things I’ve learned is taking advantage of good code that someone else has already written. By this, I don’t mean copy/pasting their code,* but rather using the existing features rather than re-inventing the wheel.
Many Perl modules have been around long enough that they come standard with Perl now. You might as well consider them “perl” itself, rather than 3rd-party development. The sign of a good Perl developer** these days is (among other things), how many modules you are proficient in, and have experience using?
Here are a few examples of helpful, and standard, Perl modules that you should know, and if you don’t know, here’s why they’re helpful. All are found on CPAN, of course, but should already be installed on your system if you’ve installed Perl:***
- strict and warnings – Both of these should be the first things you do in a Perl script after the #!. Both are needed to ensure proper scoping of variables, and also to ensure that you’re getting the right debug messages. Not all bad code causes your script to obviously fail, some will only show up in a warning.
- Data::Dumper – This cool module will print out the values in a data structure. This is very helpful for debugging problems with arrays, especially multidimensional-arrays, or other complex structures. When the script is tested and working, you can take this back out.
- Fcntl – This is a very, very important module for controlling file access among other things. The most common usage is proper file locking. Use with argument qw(:flock). You can even lock the __DATA__ filehandle which is a great way to prevent a script from running more than one instance at a time.
- Getopt::Long – This helpful module will provide GNU-compliant command-line arguments, and make your script look more professional. For any script you want to use in public (or in work), consider using this. Works with qw(GetOptions) argument.
- Net::FTP – This module provides good functionality for use with FTP. You can easily turn a Perl script into an FTP client.
- CGI – The standard CGI module. Has lots of built-in features these days to create W3C-compliant HTML headers (i.e. make your HTML pages look more professional), but even if you don’t use that, you should definitely know how to write even basic CGI pages.
- POSIX – A very complex Perl module that integrates a lot of POSIX-compliant C-language functions. Many have been deprecated by other Perl features, but some are still very useful. I’ve used the
strftime()function at least once. - File::Basename – The whole File:: set of modules is useful, but I like this module because it’s helpful for parsing long path names (from input) easily into just the part you want. Simple, but useful.
- LWP::Simple – This module acts like a very light-weight webclient. Can be used for monitoring the health of websites, or possibly to grab some data from the website. Simple, but many uses.
- Date::Manip – This is a real time-saving module for dealing with time. Instead of trying to mess with dates yourself, you shoudl definitely take advantage of this module. For writing work scripts, or managing timezone issues, this is a must.
This is not a complete list. I’d love to hear from others on suggestions. Good luck!
Namuamidabu
* – I’ve met people who said this is how they wrote Perl scripts…not a good idea. If you can’t understand the code, why are you even trying to use it?
** – A colleague of mine at work said that the hardest thing for a programmer is not learning computer languages, but accepting the fact that someone else has written code better than you, and humbling yourself enough to use it. Given the pride I see among many in IT, I think he’s on to something.
*** – Better yet, just run perldoc (module name) on your host to see how it works. Perldocs are like “man pages” for Perl.
Great post! One of the first lessons I learned (the hard way) was that just about anything you want to do has already been done by someone else. Your second footnote is right on. I was a devout Perl junkie for many years, but I’ve been doing most of my dabbling in Python over the past year. Your post has inspired me to go back and play around with Perl again (and also install the latest version)! Thanks Gerald!
I’M NOT PRIDEFUL! I’M NOT, I’M NOT, I’M NOT!
Ok…I am. *sigh* You play a tough game, buddy!
Another good module is Perl::Critic. It is a tool, not a module to use from your program.