NAME

Modern::Perl::Prelude - Project prelude for modern Perl style on Perl 5.26+

SYNOPSIS

use Modern::Perl::Prelude;

state $counter = 0;
my $s = trim("  hello  ");

try {
    die "boom\n";
}
catch ($e) {
    warn $e;
}

Flag-style optional imports:

use Modern::Perl::Prelude '-utf8';
use Modern::Perl::Prelude qw/-class -defer/;
use Modern::Perl::Prelude qw(-corinna -always_true);

Hash-style optional imports:

use Modern::Perl::Prelude {
    utf8        => 1,
    defer       => 1,
    always_true => 1,
};

Disable native pragmata/features lexically again:

no Modern::Perl::Prelude;
no Modern::Perl::Prelude '-utf8';
no Modern::Perl::Prelude { utf8 => 1 };
no Modern::Perl::Prelude '-always_true';

DESCRIPTION

This module bundles a small, opinionated set of pragmata, features, and compatibility layers for writing Perl in a Perl 5.40+-style while staying runnable on Perl 5.26+.

It enables:

  • strict

  • warnings

  • feature say, state, fc

  • Feature::Compat::Try

  • selected functions from builtin::compat

Additional compatibility layers may be requested explicitly via import options.

DEFAULT IMPORTS

This module always makes the following available in the caller's lexical scope:

say
state
fc
try / catch
blessed
refaddr
reftype
trim
ceil
floor
true
false
weaken
unweaken
is_weak

IMPORT OPTIONS

Flag-style

Supported flags:

-utf8
-class
-defer
-corinna
-always_true

Examples:

use Modern::Perl::Prelude '-utf8';

use Modern::Perl::Prelude qw(
    -class
    -defer
);

use Modern::Perl::Prelude qw(
    -class
    -utf8
    -always_true
);

Hash-style

Hash-style arguments must be passed as a single hash reference:

use Modern::Perl::Prelude {
    utf8        => 1,
    defer       => 1,
    always_true => 1,
};

Supported hash keys:

  • utf8

  • class

  • defer

  • corinna

  • always_true

For compatibility-layer options (class, defer, corinna), a true scalar enables the feature. A hash reference also enables it and is passed through to the underlying module's import.

For always_true, use a boolean value.

-utf8 / utf8

Also enables source-level UTF-8, like:

use utf8;

-class / class

Loads and imports Feature::Compat::Class into the caller scope.

This is the forward-compatible class syntax option.

-defer / defer

Loads and imports Feature::Compat::Defer into the caller scope.

-corinna / corinna

Loads and imports Object::Pad into the caller scope.

This is intended for projects that explicitly want Object::Pad / Corinna-like class syntax.

-class and -corinna are mutually exclusive.

-always_true / always_true

Enables automatic true return for the currently-compiling file via true, so modules can omit a trailing:

1;

This behavior is file-scoped rather than lexically-scoped.

OPTIONAL IMPORTS

When requested explicitly, this module can also make the following available:

  • -class / class enables class, method, field, ADJUST via Feature::Compat::Class

  • -defer / defer enables defer

  • -corinna / corinna enables class syntax via Object::Pad

  • -always_true / always_true enables automatic true return for the current file

UNIMPORT

no Modern::Perl::Prelude reliably disables native pragmata/features managed by this module:

strict
warnings
say
state
fc
utf8

Compatibility layers such as Feature::Compat::Try, Feature::Compat::Class, Feature::Compat::Defer, Object::Pad, and builtin::compat are treated as import-only for cross-version use on Perl 5.26+ and are not guaranteed to be symmetrically undone by no Modern::Perl::Prelude.

always_true is an exception: no Modern::Perl::Prelude '-always_true' or

no Modern::Perl::Prelude { always_true => 1 };

disables the automatic true-return behavior for the current file.

DESIGN NOTES

This is a lexical prelude module. It is implemented via Import::Into so that pragmata and lexical functions affect the caller's scope, not the scope of this wrapper module itself.

Optional compatibility layers are loaded lazily, only when explicitly requested.

The always_true option is implemented via true and is file-scoped.

AUTHOR

Sergey Kovalev <skov@cpan.org>

CO-AUTHOR

Kirill Dmitriev <zaika.k1007@gmail.com>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.