Modern Perl tour
use v5.36
A single line turns on the modern language. use v5.36 enables strict, warnings, and the feature bundle of that release, so the three boilerplate lines you have seen throughout this course become one.
#!/usr/bin/perl
use v5.36;
say "hello from Perl $^V";
hello from Perl v5.36.0
$^V is the version of the interpreter running the program. Set the version line to the oldest Perl you support, not the newest you have installed, otherwise the script refuses to start on a slightly older system.
If the target is older than 5.36, import the pieces individually instead: use feature "say", use feature "signatures", and keep the separate strict and warnings lines.
Signatures
With the 5.36 bundle, a subroutine declares its parameters instead of unpacking @_ by hand. Defaults and constraints are part of the declaration, and the mistake of reading the wrong argument disappears.
sub greet ($name, $greeting = "hi") {
return "$greeting, $name";
}
say greet("Ada");
say greet("Grace", "hello");
hi, Ada hello, Grace
The signature checks the argument count on every call, so too few or too many arguments fail with a clear message instead of a vague warning about an uninitialized value. A hash or array collects the rest with %opts or @rest at the end of the list.
Inside a signature, arguments are still aliases to @_, so assigning to one changes the caller's variable. Copy into my before modifying, just as you would with my ($x) = @_.
Small features, big difference
Several small features remove the most common lines of boilerplate. state keeps a variable between calls, which is a private static for a function:
sub next_id {
state $id = 0;
return ++$id;
}
say next_id() for 1 .. 3;
1 2 3
Postfix dereference reads left to right, so a nested structure no longer needs a chain of braces and arrows. ->@* gets the list, ->%* the hash, ->$#* the last index:
my $data = { tags => ["perl", "regex"] };
say for $data->{tags}->@*;
say "last index: ", $data->{tags}->$#*;
The isa operator replaces the eval { $x->isa(...) } dance, and //= assigns only when the left side is undefined, which is exactly what option defaults need:
say "a timer object" if $obj isa "Time::Piece";
$config{port} //= 8080;
Newer releases add more: builtin provides true, false and is_bool; try/catch gives structured exceptions without eval. Check the version that introduced each one before you rely on it, and keep experimental features out of code that must run for years.
Modern tooling
The language is only half of a modern setup. The other half is a repeatable way to format, lint, install and test:
- perltidy formats code, so diffs contain changes and not whitespace.
- perlcritic flags risky constructs with a named policy for each.
- cpanfile lists dependencies; carton pins exact versions for deployment.
- prove runs the test suite, and Test2 extends Test::More when you need more.
requires "perl", "5.036";
requires "JSON::PP";
requires "HTTP::Tiny";
on "test" => sub {
requires "Test::More";
};
carton install
Write the version requirement into the cpanfile, not just into the use line: the installer then refuses an interpreter that is too old, before the program starts and fails in a confusing place.
None of this changes what Perl is. It changes how the code reads to the next person, and that person is usually you in a year.
Next: Hero roadmap: the whole course on one page
Article author: Arthur Isaev