DEV Community

Mark Gardner
Mark Gardner

Posted on • Edited on • Originally published at phoenixtrap.com

5 Ways You Can Embrace the Magic of Perl

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;
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

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 10 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;
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'};
Enter fullscreen mode Exit fullscreen mode

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

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

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;
}
Enter fullscreen mode Exit fullscreen mode

In the example above, $check->(@_) returns the type-checked arguments to the foo() function courtesy of Type::Params' compile_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.

Top comments (7)

Collapse
 
grinnz profile image
Dan

But in the second line, we’re assigning to the scalar variable $bar, which now contains the number of items in the list.

This isn't correct in this example; in scalar context, the comma evaluates the expression to the left and to the right of it and returns the expression to the right. So it happens to return 3 in this case because that's the last expression. Returning the size in scalar context is behavior specific to array variables and functions like grep (but many functions do something different, as you note).

Collapse
 
choroba profile image
E. Choroba

True. I usually use strings or numbers like 101, 102 in examples and tests to not fall into this trap.

Collapse
 
mjgardner profile image
Mark Gardner

Thanks! I’ve made the correction.

Collapse
 
thibaultduponchelle profile image
Tib

Great article !

I feel like something is weird with statement paragraph ? Like a missing code snippet. Am I correct ?

Collapse
 
mjgardner profile image
Mark Gardner

No, it just builds on the previous section. Would it be clearer if I repeated the say for (1..5) thing again?

Collapse
 
thibaultduponchelle profile image
Tib

Ah yes ! That was it :) Then I would recommend to do not put statement modifier in first paragraph also

Collapse
 
kes777 profile image
Eugen Konkov

my $bar = (1, 2, 3); # scalar context, $bar contains 3

Not good example. 3 because it is last element or 3 because of count of elements?