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";

perl demo.pl

hello from Perl v5.42.3

$^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.

use v5.36; sub greet ($name, $greeting = "hi") { return "$greeting, $name"; } say greet("Ada"); say greet("Grace", "hello");

perl demo.pl

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, the parameters are copies of the arguments, so assigning to one leaves the caller's variable unchanged; that is the safer behaviour the hand-written my ($x) = @_ gave you.

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:

use v5.36; sub next_id { state $id = 0; return ++$id; } say next_id() for 1 .. 3;

perl demo.pl

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:

use v5.36; my $data = { tags => ["perl", "regex"] }; say for $data->{tags}->@*; say "last index: ", $data->{tags}->$#*;

perl demo.pl

perl
regex
last index: 1

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:

use v5.36; use Time::Piece; my $obj = localtime; say "a timer object" if $obj isa "Time::Piece"; my %config; $config{port} //= 8080; say "port: $config{port}";

perl demo.pl

a timer object
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:

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.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does use v5.36 turn on?

    Answer

    strict, warnings and the feature bundle of that release

    The single line replaces the three boilerplate lines and enables the features that release added.

  2. Which version should the use v5.NN line name?

    Answer

    The oldest Perl you support

    A higher number makes the script refuse to start on an older system, even when it would run there.

  3. What does state $id = 0; inside a subroutine do?

    Answer

    It keeps the value between calls, like a private static

    The initialisation runs once, and the next call sees the value the previous call left.

  4. What does the postfix ->@* on a reference give you?

    Answer

    The list of elements

    The form reads left to right and replaces the older @{ $ref } spelling of the same list.

  5. What does $config{port} //= 8080; do?

    Answer

    It assigns 8080 only when the value is undefined

    The defined-or assignment keeps a value that is already there, which is what an option default needs.

  6. What does the isa operator replace?

    Answer

    The eval { $x->isa(...) } dance around a method call

    The operator answers the class question without an eval, and it is false rather than fatal on a plain value.

  7. What does this program print? use v5.36; sub greet ($name, $greeting = "hi") { return "$greeting, $name"; } say greet("Ada");

    Answer

    hi, Ada

    The second parameter falls back to its default, and the subroutine returns the greeting before the name.

  8. What does this program print? use v5.36; sub next_id { state $id = 0; return ++$id; } say next_id() for 1 .. 2;

    Answer

    1 / 2

    The state variable keeps its value, so the second call increments the result of the first.

  9. What does this program print? use v5.36; my $data = { tags => ["a", "b"] }; say for $data->{tags}->@*;

    Answer

    a / b

    The postfix dereference gives the list of tags, and say prints each one on its own line.

  10. What does this program print? use v5.36; my %config; $config{port} //= 8080; $config{port} //= 9090; say $config{port};

    Answer

    8080

    The first assignment fills the undefined key, and the second one finds a defined value and leaves it alone.

Article author: Arthur Isaev