Home / Alt manpages / perl5360delta(1)

  • perl5360delta(1)
  • User command
  • linux

Use Perl 5.36's Safer Defaults and New Syntax

You will test the Perl 5.36 feature bundle, write a small subroutine with a signature, use the new -g whole-input flag, and identify which newer features are still experimental. The examples are read-only and leave no files behind. Allow about 15 minutes if Perl is already installed.

This guide follows perl5360delta(1), the release note for Perl 5.36.0. The installed manpage on this machine is supplied by perl-doc version 5.38.2-3.2ubuntu0.6 and is rendered for Perl 5.38.2. That means the examples can be checked locally, but the document describes what became available in 5.36.0, not the complete change set in the later interpreter.

1. Check the interpreter before changing code

Start by recording which Perl your shell will run:

$ command -v perl
/usr/bin/perl
$ perl -e 'printf "%vd\n", $^V'
v5.38.2

The release note compares 5.36.0 with 5.34.0. A later Perl normally retains these features, but it can also change experimental behaviour. If a deployment must stay on 5.36 semantics, check the actual interpreter in that deployment rather than relying on the version installed on your workstation.

Checkpoint: the version command should print the interpreter that will run your script. If it prints an unexpected path or version, stop here and fix the environment first. Do not install another Perl into a shared system merely to follow this example.

2. Turn on the 5.36 feature bundle

Put use v5.36; near the top of a script that requires the release's language defaults:

use v5.36;

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

print add(2, 3), "\n";

Run it without creating a source file by passing the same code to -e:

$ perl -e 'use v5.36; sub add ($x, $y) { $x + $y }; print add(2, 3), "\n"'
5

In this bundle, subroutine signatures are stable and the isa operator is stable. The bundle also enables warnings, and disables the indirect and multidimensional features. It is similar to adding stricter language defaults, but it is not a substitute for reviewing the code that is being upgraded.

A signature is not the same as a type declaration. $x and $y still hold ordinary Perl values, and the example only adds positional argument binding. Avoid mixing a signature with @_ or $_[0] while upgrading: the 5.36 release note marks access to that legacy argument array inside a signatured subroutine as experimental, with its own warning category.

3. Use -g when a filter needs the whole input

Perl 5.36 adds -g as a shorter alias for -0777. It makes the input record separator undefined, so a filter reads the complete input as one string instead of processing one line at a time.

$ printf 'alpha\nbeta\n' | perl -g -pe 's/\n/|/g'
alpha|beta|

The same result comes from the older spelling:

$ printf 'alpha\nbeta\n' | perl -0777 -pe 's/\n/|/g'
alpha|beta|

This is useful for a deliberately small whole-document transformation. It is not a free performance improvement: the input must fit in memory as one record. Do not point a -g one-liner at an unbounded log, device stream or user-uploaded file without an explicit size limit. For large input, use a streaming design or a tool that can process bounded chunks.

Checkpoint: compare the two commands and confirm that both produce one transformed string. If your filter relies on per-line state, do not replace its normal record handling with -g.

4. Try the new builtin functions carefully

Perl 5.36 adds the core builtin module and functions such as reftype, trim, indexed, true, false, is_bool, weaken, blessed, ceil and floor. The module and its functions are experimental in this release, so opt in knowingly and expect warnings by default.

$ perl -we 'use v5.36; use builtin "reftype"; print builtin::reftype([]), "\n"'
Built-in function 'builtin::reftype' is experimental at -e line 1.
ARRAY

The warning is useful during evaluation. Suppressing it is a local decision, not proof that the interface is stable:

use v5.36;
use builtin 'reftype';
no warnings 'experimental::builtin';

print reftype([]), "\n";

Use the documented lexical import form when you want a short name. Keep experimental calls behind a small interface in application code so a future Perl upgrade has one place to review. Do not treat builtin::true and builtin::false as a general strict Boolean type system; Perl values remain flexible, even though the new Boolean tracking can preserve their Boolean nature.

5. Treat defer, try and multi-value loops as experiments

The release also adds several experimental syntax features. A defer block runs when its enclosing scope exits, after execution reaches the block. It is not run if control leaves the containing scope before the declaration is reached.

$ perl -we 'use v5.36; use feature "defer"; no warnings "experimental::defer"; { defer { print "cleanup\n" }; print "body\n" }'
body
cleanup

A defer block cannot be escaped with return, goto or loop control. That restriction matters when adapting cleanup code: test the control-flow paths rather than assuming it behaves like a destructor.

Experimental try/catch syntax can now have a finally block:

$ perl -we 'use v5.36; use feature "try"; no warnings "experimental::try"; try { die "bad\n" } catch ($e) { print "caught:$e" } finally { print "finally\n" }'
caught:bad
finally

Do not use these constructs in a compatibility-sensitive module without testing the oldest supported Perl. The same caution applies to multi-value iteration such as for my ($key, $value) (%hash), which the release note marks experimental and associates with the experimental::for_list warning.

6. Check upgrade traps before committing

A physically empty sort is now a compile-time error. These two forms no longer provide a harmless empty list:

@items = sort;      # compile-time error in 5.36
@items = sort ();   # compile-time error in 5.36

Use an explicit empty list if that is what the code means, or provide the real list to sort. Compile the whole program in CI, because a runtime test that never loads the affected branch will not catch a compile-time error.

Also review nested version declarations. After use v5.11 or later, a later use v5.8 emits a deprecation warning. If genuinely different language versions must coexist, isolate them in separate lexical blocks. This is usually a sign that the upgrade needs a clearer module boundary.

Done means

  • You verified the Perl executable and version used by the test.
  • use v5.36 compiled a signatured subroutine and produced the expected result.
  • You understand that -g reads all input into one record and used it only for bounded data.
  • Experimental builtin, defer, try and multi-value loop features remain visibly identified and tested.
  • You checked for empty sort expressions and problematic nested use VERSION declarations.