Category: Perl

Moose objects and roles

Mr Moose’s Newfangled Object Framework Creating objects from scratch by hand-rolling them is somewhat fragile, and you end up drowning in the writing of endless boilerplate accessor methods. I would strongly suggest that rather than doing objects the hard way, you use the Moose object framework unless there’s some particular constraint (speed, memory footprint) on …

Continue reading

Classes and objects

Mr Wall’s Olde Fashioned Objeckts Caveat lector. Despite some cosmetic surgery, this post, like the previous one on writing modules, may show its age. It was originally written when Perl 5 wasn’t even 10 years old, and things have moved on substantially since then (and no, I don’t mean Perl 6). However, there’s a lot of …

Continue reading

Packages and writing modules

Modularisation is a virtue The previous post showed you how to install and use other people’s modules; this post will address how to write your own. At some point, you will probably find yourself copying-and-pasting code from one script to another. When you find yourself doing that, you should consider what would happen if it …

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

Load more