Saturday, 1 August 2026

Defensive Perl

Perl is less fashionable than Python these days, but it is a very flexible language which excels in text processing.

Its flexibility allows a myriad of programming styles, however, so having a set of defensive techniques helps.

Here are some top tips:
  • Switch warnings on - use the minus-w command line flag -  #! perl -w
  • Learn and use perlpod - it looks a bit like this at the start of your file  =head1 NAME  myscript =cut
  • Always use strict; at the start of your program (this pragma restricts expressions that are hard to debug)
  • Qualify your variables with type prefixes (similar to "Hungarian" notation) e.g. my $sHeader for strings, my @aCodes for arrays, my %hCodesToDescriptors, and appropriate equivalents for common custom types
Recap on pragmas: A pragma is a module which influences some aspect of the compile time or run time behaviour of Perl, such as strict or warnings. From Perl 5.10 onward - custom pragmata are supported.

Happy Perling!

No comments: