Pragmas and Modern Perl

Requires declared variables, no symbolic refs, no barewords Warns about questionable constructs (lexically scoped, by category) Source code is UTF-8

Enables new features (say, state, signatures, try, class, defer,...) (CPAN/core) Enable an experimental feature and silence its warning in one line Built-ins die on failure Set up inheritance bigint / bignum / bigrat Default I/O layers Regex debugging and flags Control the sort algorithm Verbose explanations of warnings Core utility functions (5.36+) less / vars / subs

use v5.36;      # = use strict; use warnings; use feature qw(say state signatures isa current_sub fc
                #   evalbytes unicode_strings unicode_eval postderef_qq bitwise ...);
                #   also disables indirect object syntax and bareword filehandles (except STDIN etc.)
use v5.40;      # adds: try/catch stable, builtin functions imported (true, false, blessed, trim, ...), __CLASS__

26.1 builtin Functions (5.36+, Many Stable in 5.40)

use v5.40;     # imports the stable builtin bundle
use builtin qw(true false is_bool weaken unweaken is_weak blessed refaddr reftype
               ceil floor trim indexed created_as_string created_as_number);
my $flag = true;
say is_bool($flag) ? 'boolean' : 'not boolean';
say ceil(2.1), floor(2.9), trim("  hi  ");
for my ($i, $item) (indexed 'a', 'b') { say "$i=$item" }

26.2 Other Modern Features

Modern Perl books and the Modern::Perl / common::sense / strictures modules promote these defaults.

Removed or deprecated: given/when and smartmatch (~~), indirect object syntax (new Foo), $*/$# variables, dump, unescaped { in some regexes, Perl 4-style ' package separator (Foo'Bar, deprecated in 5.38, removal planned).

use feature 'defer';                         # 5.36 (experimental): runs at scope exit
{
    open my $fh, '<', $file or die;
    defer { close $fh; say 'closed' }
    ...
}
say "Version: $^V";                          # v5.40.0
say 'isa works' if $obj isa My::Class;       # isa operator
my $n = 1_000;