October 2012 archive

Glockentierchen

Vorticella micrograph [CC-BY-SA-3.0 Steve Cook]

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

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 = …

Continue reading

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 …

Continue reading

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 …

Continue reading

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 …

Continue reading

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” …

Continue reading

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, …

Continue reading

Load more