Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Perl in 2026

Latest stable release of perl is 5.44.0. Released on 2026-07-15.

A lot of code is stuck in 5.10.0 released on 2007-12-18.

See the releases of Perl

perldelta

The details of the changes are published in the perldelta documents.


Docker

Images

Run

docker run -it --rm --workdir /opt -v$(pwd):/opt perldocker/perl-tester:5.44 bash
docker run -it --rm --workdir /opt -v$(pwd):/opt --user ubuntu szabgab/perl:latest bash'

strict and warnings

use v5.44;
$x = 19;
$y = 23;

print "$x $y $z\n";

use v5.44;

# $x = 19;
my $x = 19;
my $y = 23;
#my $z = $x + $y;

my $z;
say $z;

Function signatures


  • Positional parameters
  • Named named parameters (experimental feature)

  • Required parameters.
  • Optional parameters with default values.

  • Slurpy parameters

Plain function

#use v5.44;
use feature 'say';
#use strict;
#use warnings;

sub area {
    my ($width, $height) = @_;
    return $width * $height;
}

my $area = area(2, 3);
say $area;

my $too_few = area(2);
say $too_few;

my $too_many = area(2, 5, 21);
say $too_many;

  • We have use feature 'say'; to add the say function without enabling any other feature. Nothing is reported.
  • We can turn on use strict; and use warnings;. The latter will generate a warning.
  • Alternatively it is enough to write use v5.44; and it turn on all the feautes.

Function with signature

use v5.44;

sub area($width, $height) {
    return $width * $height;
}

my $area = area(2, 3);
say $area;

my $too_few = area(2);
say $too_few;

my $too_many = area(2, 5, 11);
say $too_many;

  • Checks that the correct number of parameters is provided.
  • Raises run-time exception.

Optional parameters

use v5.44;

sub area($width, $height = 10) {
    return $width * $height;
}


my $area = area(2, 3);
say $area;

my $few = area(2);
say $few;

my $too_many = area(2, 5, 11);
say $too_many;

Autoincrement

use v5.44;

my $auto_id = 0;
sub increment($thing, $id = $auto_id++) {
    say "$thing has ID $id";
}

increment("table");
increment("chair");
increment("window", 10);
increment("doo0r");

Reuse parameter

use v5.44;

sub greet($first_name, $surname, $nickname = $first_name) {
    say qq{Hello $first_name $surname is known as "$nickname"};
}

greet("Foo", "Bar");
greet("Gabor", "Szabo", "szabgab");

Slurpy parameters

use v5.44;

use Data::Dumper qw(Dumper);


sub mymap($function, @inputs) {
    my @results;
    push @results, $function->($_) foreach @inputs;
    return @results;
}

{
    my @response = mymap( sub {  2 * shift }, 3, 4);
    say Dumper \@response;
}

{
    my @response = mymap( sub {  2 * shift });
    say Dumper \@response;
}


Slurpy hash parameters

use v5.44;

use Data::Dumper qw(Dumper);

sub add_header($thing, %header) {
    say $thing;
    say Dumper \%header;
}


add_header("Hello");
add_header("Key-value pairs", machine => "localhost", signature => "Foo Bar" );

add_header("Just values", "machine", "localhost", "signature", "Foo Bar" );

my %data = (
    machine => "localhost",
    signature => "Foo Bar",
);
add_header("Hash", %data);

Named parameters

use v5.44;
no warnings "experimental::signature_named_parameters";

sub pow(:$base, :$exponent) {
    return $base ** $exponent;
}

# say pow(2, 3);
say pow(base => 2, exponent => 3);
say pow(exponent => 3, base => 2);

my %params = (
    base => 2,
    exponent => 4,
);
say pow(%params);

Named optional parameters

use v5.44;
no warnings "experimental::signature_named_parameters";

sub run(:$cmd, :$hostname = "localhost") {
    say "Running '$cmd' on '$hostname'";
}

run(cmd => "ls");
run(cmd => "cat", hostname => "animals.com");
run(hostname => "animals.com", cmd => "dog");

OOP with class

perlclass described the feature available since 5.38 (released on 2023-07-02). It is still marked as experimental.

Useless point

#use v5.44;
use v5.38;
use feature 'class';
no warnings 'experimental::class';
use Data::Dumper;

class Point {
    field $x;
    field $y;

}
my $p = Point->new();
say $p;
#say Dumper $p;

#my $q = Point->new(x => 2, y => 3);

Accessors: setter, getter

#use v5.44;
use v5.38;
use feature 'class';
no warnings 'experimental::class';

class Point {
    field $x :reader :writer;
    field $y :reader(get_y) :writer(add_y);
}

my $p = Point->new();
# say $p->x; # Undefined

$p->set_x(10);
say $p->x;

$p->add_y(7);
say $p->get_y;

Constructor with parameters

#use v5.44;
use v5.38;
use feature 'class';
no warnings 'experimental::class';

class Point {
    field $x :param;
    field $y :param;
}
my $p = Point->new(x => 2, y => 3);

say $p;

Methods

#use v5.44;
use v5.38;
use feature 'class';
no warnings 'experimental::class';

class Point {
    field $x :param;
    field $y :param;

    method stringify {
        return "($x, $y)";
    }

    method move($dx, $dy) {
        $x += $dx;
        $y += $dy;
    }
}
my $p = Point->new(x => 2, y => 3);
say $p->stringify;

$p->move(7, 8);
say $p->stringify;

ADJUST

#use v5.44;
use v5.38;
use feature 'class';
no warnings 'experimental::class';

class Point {
    field $x :param;
    field $y :param;

    ADJUST {
        $y = 23;
        #$y += 23;
    }

    method stringify {
        return "($x, $y)";
    }

}

my $p = Point->new(x => 2, y => 19);
say $p->stringify;

ADJUST is called after the instance was created and the attributes were populated.

Unicode

use feature 'say';

say "Gábor Szabó";
use v5.44;
use utf8;
binmode STDOUT, ":utf8";

say "Gábor Szabó";