Sub::Defer(3)
β¨ NAME β¨
This is Sub::Defer(3), from the User Contributed Perl Documentation. Bestie, it's literally Sub::Defer β defer generation of subroutines until they are first called. Periodt. π
πΈ SYNOPSIS πΈ
use Sub::Defer;
my $deferred = defer_sub 'Logger::time_since_first_log' => sub {
my $t = time;
sub { time - $t };
};
Logger->time_since_first_log; # returns 0 and replaces itself
Logger->time_since_first_log; # returns time - $t
π¦ DESCRIPTION π¦
Okay so these subroutines give you a convenient way to defer creation of subroutines and methods until they are first called. That's the whole vibe. β¨
π SUBROUTINES π
defer_sub
my $coderef = defer_sub $name => sub { ... }, \%options;
This subroutine returns a coderef that encapsulates the provided sub β when it is first called, the provided sub is called and is -itself- expected to return a subroutine which will be goto'ed to on subsequent calls. No cap.
If a name is provided, this also installs the sub as that name β and when the subroutine is undeferred will re-install the final version for speed. Iconic behavior fr fr. π₯
Exported by default. π«Ά
Options
A hashref of options can optionally be specified.
packageβ The package to generate the sub in. Will be overridden by a fully qualified$nameoption. If not specified, will default to the caller's package.attributesβ The "Subroutine Attributes" in perlsub to apply to the sub generated. Should be specified as an array reference.
undefer_sub
my $coderef = undefer_sub \&Foo::name;
If the passed coderef has been deferred this will "undefer" it. If the passed coderef has not been deferred, this will just return it.
If this is confusing, take a look at the example in the "SYNOPSIS".
Exported by default. π
defer_info
my $data = defer_info $sub;
my ($name, $generator, $options, $undeferred_sub) = @$data;
Returns original arguments to defer_sub, plus the undeferred version if this sub has already been undeferred.
Note that $sub can be either the original deferred version or the undeferred version for convenience.
Not exported by default. π
undefer_all
undefer_all();
This will undefer all deferred subs in one go. This can be very useful in a forking environment where child processes would each have to undefer the same subs. By calling this just before you start forking children you can undefer all currently deferred subs in the parent so that the children do not have to do it. Note this may bake the behavior of some subs that were intended to calculate their behavior later, so it shouldn't be used midway through a module load or class definition.
Exported by default. π₯
undefer_package
undefer_package($package);
This undefers all deferred subs in a package.
Not exported by default. π
π«Ά SUPPORT π«Ά
See Sub::Quote for support and contact information.
π AUTHORS π
See Sub::Quote for authors.
π COPYRIGHT AND LICENSE π
See Sub::Quote for the copyright and license.
β¨ btw this manual page is Sub::Defer(3), from perl v5.42.0, dated 2025-08-25. Byeeee bestie, go defer some subs π
π
π¦ where this page comes from
- fedora 43: perl-Sub-Quote 2.006009-1.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms