Add Moose-style attribute accessors to your Perl classes
Writing accessors in vanilla object oriented Perl doesn't have to lead to verbose boilerplate code
How to redirect and restore STDOUT
STDOUT is the Perl filehandle for printing standard output. Unless a filehandle is specified, all standard printed output in Perl will go to the terminal. Because STDOUT is just a global variable, it can be redirected and restored. Want to implement logging on a program without changing every print statement in the source code? Want to capture the standard output of a perl CRON job? Read on.
How to read a string into an array of characters using split
Perl's split function has a useful feature that will split a string into characters. This works by supplying an empty regex pattern ("//") to the split function. This can be used to easily split a word into an array of letters, for example:
Banish unsightly variable assignments with Method::Signatures
Add subroutine signatures to Perl
Use the logical-or and defined-or operators to provide default subroutine variable behaviour
Perl subroutines do not have signatures so variables must be initialized and arguments assigned to them inside the subroutine code. This article describes two useful shortcuts that can simplify this process.
Perl hash basics: create, update, loop, delete and sort
Hashes are one of Perl's core data types. This article describes the main functions and syntax rules for for working with hashes in Perl.
Old School Object Oriented Perl
If you need to write object oriented Perl code with no dependencies, then you need to use the old school Perl syntax. This article describes the main features of old school object oriented Perl including class declaration, constructors, destructors, methods, attributes, accessors and inheritance.
Dynamic variable names with a dereferencing block
Perl is remarkably flexible and allows you to achieve all kinds of wizardry with the language. One example of this is using a dereferencing block to use a scalar value as a variable name. This allows you to use variables with dynamic names.
The conditional (ternary) operator
One way to reduce the verbosity of Perl code is to replace if-else statements with a conditional operator expression. The conditional operator (aka ternary operator) takes the form: <b>logical test</b> ? <b>value if true</b> : <b>value if false</b>.
Test if the user is root
When Perl is executing a program, it maintains the user id of the process owner in a global variable ($<). When a Perl program is executed by root or a user with root privileges (e.g. using the sudo command), the user id variable is always set to zero. This can be checked at the command line:
Upgrade your list printing using field separator variables
A typical way to print every element of an array in Perl is using a foreach loop:
Repeat strings with the repetition operator Repeat strings with the repetition operator
You get the idea - Perl has a repetition operator (x) that repeats the scalar or list on its left by the number on it's right (like multiplication).
Perl string functions - concatenate substring and split
Perl has many string functions, let's take a look at a some of the most common ones: concatenate, substring and split.
Quoting strings in Perl - even ones containing apostrophes and quote or speech marks
Broadly speaking Perl has two types of strings: quotes that are interpolated at runtime and literal quotes that are not interpolated. Let's review each of these in turn.