Perl 5.36 is released - warnings enabled, subroutine signatures, try-catch-finally statements, Unicode 14 support

Perl 5.36 was released on May 28, 2022. Enabling warnings, subroutine signatures, try-catch-finally statements, Unicode 14 support, and the functions to distinguish between booleans, strings, and numbers.

Use Perl 5.36 Features

To use all the features of Perl 5.36, put the version declaration "use v5.36" at the beginning of the source code.

use v5.36;

The version declaration should be written at the beginning of the source code. Otherwise, unexpected behavior may occur.

Enable Warnings

The warnings module is enabled by the version declaration.

use warnings;

The strict module is already enabled by the version declaration, so in Perl 5.36 the version declaration enables strict and warning.

# From now
use strict;
use warnings;

# Perl 5.36+
use v5.36;

If Perl 7 is released in the future, you can omit the minor version:

use v7;

Subroutine signature is no longer experimental

Subroutine signature is no longer experimental and is enabled by the version declaration. Subroutine signature is the feature to write arguments after a subroutine name.

use v5.36;

sub add ($x, $y) {
  return $x + $y;
}

You can write subroutines as follows.

# From now
sub add ($x, $y) {
  my ($x, $y) = @_;
  
  $y = 3 unless defined $y;
  
  return $x + $y;
}

# v5.36
sub add ($x, $y = 3) {
  return $x + $y;
}

Note that a subroutine signature checkes the number of arguments. If there are few or many arguments given, an exception will be thrown.

If you want to disable the number of arguments check, please write as follows.

sub add ($x, $y, @) {
  return $x + $y;
}

Experimental try-catch-finally Statement

You can use experimental try-catch-finally statement.

use feature'try';

try {
  
}
catch ($e) {
  
}
finally {
  
}

"-g" Command Line Flag

An option "-g" is added to the perl command. This option reads a whole file .

perl -n -g -E'if (m | <h1> (. *?)</h1> |) {say $1;}' a.html

Previously, Perl do the same thing using the option "-0777", but this feature is used so often, an easy way is added.

perl -n -0777 -E'if (m | <h1> (. *?)</h1> |) {say $1;}' a.html

Unicode 14.0 Support

Unicode 14.0 is now supported.

builtin Namespace

Experimentally "builtin" namespace is added for standard functions. Perl can add functions without worrying about name clashes.

my $true = builtin::true;

use builtin 'true';

my $true = true;

All functions in the builtin namespace are currently experimental.

true Function, false Function, and is_bool Function

"builtin::true" and "builtin::false" functions are added to represent boolean values.

# True
builtin::true;

# False
builtin::false;

"builtin::true" is equal to "!!1" and builtin::false is equal to "!!0".

You can check the boolean value with "builtin::is_bool" function.

if (builtin::is_bool $bool) {
  
}

This is useful when generating JSON.

You can distinguish between numbers and strings

In Perl 5.36, you can use the "builtin::created_as_number" and "builtin::created_as_string" functions to distinguish between numbers and strings. It is supposed to be used in serializers such as JSON.

if (builtin::created_as_number $data) {
  
}

if (builtin::created_as_string $data) {
  
}

The combination with "builtin::is_bool" allows correct JSON serialization and deserialization using only the Perl core functions.

if (builtin::is_bool $data) {
  
}
eleif (builtin::created_as_number $data) {
  
}
elsif (builtin::created_as_string $data) {
  
}

builtin::trim Function

A builtin::trim function is added. You can remove the leading and trailing spaces.

my $trim = builtin::trim $string;

Iterating multiple elements

The "for" statement supports the iteratating multiple elements. This feature is experimental.

for my ($key, $value) (%hash) {...}
for my ($left, $right, $grinding) (@moties) {...}

How to install Perl 5.36 quickly?

perlbrew or plenv is easy ways to install Perl 5.36.

perlbrew

perlbrew install perl-5.36.0

plenv

plenv install 5.36.0

See all changes

See perldelta to see all the changes.

perldelta

You can past release information.

Perl Release Information

My Comments

Congratulations on the release of Perl 5.36! Thank you to all the developers.

"use warnings" is enabled by the version declaration. I want to use the version declaration more.

I want to use "for" syntax that iterates multiple elements.

The "-g" option makes one-liner easier.

"trim" seems to be used frequently.

It is very good to be able to distinguish between strings, numbers and booleans.

Checking the number of arguments in a subroutine signature is a bit worrisome when used in a production environment for products that require high reliability, as it raises unexpected run-time exceptions.

Related Informatrion