person doing card trick

Perl is said (sometimes frustratingly) to be a do-​what-​I-​mean programming language. Many of its statements and constructions are designed to be forgiving or have analogies to natural languages. Still others are said to be magic,” behaving differently depending on how they’re used. Adept use of Perl asks you to not only understand this magic, but to embrace it and the expressiveness it enables. Here, then, are five ways you can bring some magic to your code.

$_

Perl has many special variables, and first among them (literally, it’s the first documented) is $_. Also spelled $ARG if you use the English module, the documentation describes it as the default input and pattern-​matching space.” Many, many functions and statements will assume it as the default or implicit argument; you can find the full list in the documentation. Here’s an example that uses it implicitly to output the numbers from 1 to 5:

say for 1 .. 5;

Output:

1
2
3
4
5

Where some languages require an iterator variable in a for or foreach loop, in the absence of one Perl assigns it to $_.

Statement modifiers

We then use our second trick; where some other languages require a block to enclose every loop or conditional (whether denoted by braces { } or indentation), Perl allows you to put said looping or conditional statement after a single other statement, in this case the say which prints its argument(s) followed by a newline.

However, above we have no arguments passed to say and so once again the default $_ is used, now containing a number from 1 to 5 which is then printed out. It’s a very powerful and expressive idiom, enabling both the writer and reader of code to concentrate on the important thing that’s happening. It’s also entirely optional. You can just as easily type:

for my $foo (1..5) {
    say $foo;
}

But where’s the magic in that?

Magic variables and use English

We mentioned the $_ variable above, and that it could also be spelled $ARG if you add use English to your code. It can be hard to read code with large amounts of punctuation, though, and even harder to remember what each variable does. Thankfully the English module provides aliases, and the perlvar man page lists them in order. It’s much easier to read and write things like $LIST_SEPARATOR, $PROCESS_ID, or $MATCH rather than $", $$, and $&, and goes a long way towards reducing Perl’s reputation as a write-​only language.

List and scalar contexts

Like natural languages, Perl has a concept of context” in which words mean different things depending on their surroundings. In Perl’s case, expressions may behave differently depending on whether they expect to produce a list of values or a single value, called a scalar. Here’s a trivial example:

my @foo = (1, 2, 3); # list context, @foo contains the list
my $bar = (1, 2, 3); # scalar context, $bar contains 3

In the first line, we assign the list of numbers (1, 2, 3) to the array @foo. But in the second line, we’re assigning to the scalar variable $bar, which now contains the last item in the list.

Here’s another example, using the reverse function:

my @foo = ('one', 'two', 'three');
my @bar = reverse @foo; # @bar contains ('three', 'two', 'one')
my $baz = reverse @foo; # $baz contains 'eerhtowteno'

In list context, reverse takes its arguments and returns them in the opposite order. But in scalar context, it concatenates all of the arguments together and returns a string with the characters in opposite order.

In general, there is no general rule for deducing a function’s behavior in scalar context from its behavior in list context.” (Dominus 1998) You’ll just have to look up the function to determine what it does, though in general, it does what you want, but if you want to force scalar context use the scalar operator:

my @foo = ('aa', 'aab', 'bbc');
my @bar = scalar grep /aa/, @foo; # returns a list (2), counting the number of matches

Hash slices

One of Perl’s three built-​in data types is the hash, also known as an associative array. It’s an unordered collection of scalars indexed by string, rather than the numbers used by normal arrays. It’s a useful construct, and you can develop complicated data structures using just scalars, arrays, and hashes. What’s not widely known is that you can access several elements of of a hash using a hash slice, using syntax that’s similar to array slices. Here’s an example:

my ($who, $home) = @ENV{'USER', 'HOME'};

It works the other way, too: you can assign to a slice.

@colors{'red', 'green', 'blue'} = (0xff0000, 0x00ff00, 0x0000ff);

I use this a lot when assigning arguments received from functions or methods (see my previous article on subroutine signatures):

use v5.24; # for postfix dereferencing
use Types::Standard qw(Str Int);
use Type::Params 'compile_named';

foo('hello', 42);

sub foo {
    state $check = compile_named(
        param1 => Str,
        param2 => Int, {optional => 1},
    );
    my ($param1, $param2) =
        $check->(@_)->@{'param1', 'param2'};

    say $param1, $param2;
}

In the example above, $check->(@_) returns the type-​checked arguments to the foo() function courtesy of Type::Paramscompile_named() function. It’s returned as a hash reference, and since hashes are unordered, we specify the order in which we want the values by dereferencing and then slicing the resulting hash. The postfix dereferencing syntax was added in Perl 5.20 and made a default feature in 5.24, and reduces the number of nested brackets and braces we have to deal with.

Conclusion

I hope this article has given you a taste of some of the magic available in the Perl language. It’s these sort of features that make programming in it a bit more joyful. As always, check the documentation for complete information on these and other topics, or look for answers and ask questions on PerlMonks or Stack Overflow.

This article is fairly well-​written, especially since it’s coming from a non-​developer perspective. If I have one nit to pick, it is that Perl does have function/​method signatures, and has had them since Perl 5.20 in 2014. See the perlsub man page for details. [Edit: This was in response to a previous version of the article. Pritesh has since amended it.]

I’d like to make my own article that takes the professional Perl developer’s (hi, that’s me!) perspective. Coming soon!