Glockentierchen
It’s nice to find out that - old and jaded as I am - biology can still delight me.
Earlier this week, I covered a first-year practical teaching microscopy skills to first-years, through the medium of pond water. As well as the usual desmids, rotifers, Daphnia and nematodes, one lucky student found a little clump of *Vorticella *(or some near relative, I’m not a protistologist!)
Installing modules
CPAN
With bits and bobs, we’ve covered much of the core functionality of Perl, but perl also comes with dozens of useful modules in its standard distribution. CPAN expands this with an enormous wealth of other contributed modules that you can install. There are three ways of doing this:
The most direct method is to go directly to CPAN, download the module you want (as a zipped tarball), unzip it and untar it, change directory to the folder you’ve just unzipped, and build the module with dmake or whatever make program you have available. So, if we wanted to install the Parse::RecDescent module, you’d download the Parse-RecDescent-1.967009.tar.gz tarball from CPAN, and then:
Command line
Command line Perl
Despite being a suitable for large projects, Perl grew out of Unix shell scripting, so it allows you to run it from the command line directly:
perl -e "print 'Hello'; print 2 + 2;"if you’re on Windows, or:
perl -e 'print q:Hello:; print 2 + 2;'if you’re on Unix. The quotes matter. The -e is the execute switch, and there are several others:
Debugging
Debuggering
So now you know how Perl works, and how to use it both for scripts and one-liners. But what do you do when it doesn’t? And how do you use it for larger projects?
Perl has some bugs and misfeatures, but it’s extremely unlikely that you’ve found a new one that’s not in the docs, so if a program fails to run as you expect, the chances are it’s you that’s buggered up. How can you find out where you’ve inserted a problem?
Bits and bobs
This post is a bit of a rag-bag of useful stuff that doesn’t fit elsewhere.
Creating code at runtime
There’s another way of creating code on the fly, besides closures. This is eval. eval comes in two flavours: string and block. The block eval looks like:
$number = <STDIN>;
my $answer;
eval {
$answer = 2 / $number;
};
print $@ ? "Divide by zero error or similar: $@" : $answer;This is a useful (and indeed the only way) to catch exceptions, that is, divide-by-zero errors and their ilk. Note you need a ; at the end of the eval { BLOCK }; because eval is a statement. If something goes wrong in the block, the special variable $@ is set with what went wrong. So eval-ling a block allows you to test Perl code, and make fatal things non-fatal.
References and data structures
References
Arrays and hashes and lists and the manipulation thereof are very useful, but sometimes you want something a bit less flat; a little more multidimensional. Perl does indeed allow for multidimensional arrays (of a sort), but the syntax is a little odd. Let us enter the world of the reference.
References are like signposts to other data. To create a multidimensional array, what you actually need to do is create a normal 1-dimensional array whose elements are these signposts to other arrays. In this way, we can fake a multidimensional array. So, how do we create a reference? There are two ways. The first is to use the \ operator, which creates references to bits of data you have named and defined previously:
Substitutions, splitting and joining
Substitution and transliteration
Matching patterns is very useful, but often we want to do something more than just match things. What if you want to replace every occurrence of a certain thing with something else? This is the domain of the s/// and tr/// operators. s/// is the substitution operator, and tr/// is the transliteration operator. tr/// is useful for simple things:
Regexes
Regular expressions
Before we looked at file manipulation, we covered how to write comparisons with conditionals:
if ( $string eq "Something we're interested in" ) {
print "Ha, ha!";
}
else {
print "Boring";
}What happens if there’s more than one thing you’re interested in though? Writing a gigantic if/elsif/else structure or even a given/when switch will make your head spin, and you’ll never be sure you’ve got every possible version of the thing you’d like to match. Take for example, matching something as simple as a letter, number or underscore character:
Files and directories
Reading and writing to files
The symbol table is a little esoteric, so let’s get back to practicalities. How do you mess about with files and directories in Perl? A simple example:
#!/usr/bin/perl
use strict;
use warnings;
open my $INPUT, "<", "C:/autoexec.bat"
or die "Can't open C:/autoexec.bat for reading $!\n";
open my $OUTPUT, ">", "C:/copied.bat"
or die "Can't open C:/copied.bat for writing $!\n";
while ( <$INPUT> ) {
print "Writing line $_";
print $OUTPUT "$_";
}Here we open two files, one to read from, one to write to. The $INPUT and $OUTPUT are filehandles, just like STDIN was, only we have created these two ourselves with open. It’s a good idea to give filehandles uppercase names, as these are less likely to conflict with Perl keywords (we don’t want to try reading from a filehandle called print for example).
Symbol table
Symbols
That’s pretty much everything for hashes, except for one topic usually missed out from introductory tutorials (possibly rightly!) This post will tell you a little about the innards of what you’ve been doing when you create variables. It’s not really necessary to know this stuff to be able to use Perl for day-to-day stuff, so do feel free to skip to the next post if this one becomes too esoteric.
