.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Sub::Defer 3pm" .TH Sub::Defer 3pm "2018-12-09" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Sub::Defer \- Defer generation of subroutines until they are first called .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Sub::Defer; \& \& my $deferred = defer_sub \*(AqLogger::time_since_first_log\*(Aq => 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 .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" These subroutines provide the user with a convenient way to defer creation of subroutines and methods until they are first called. .SH "SUBROUTINES" .IX Header "SUBROUTINES" .SS "defer_sub" .IX Subsection "defer_sub" .Vb 1 \& my $coderef = defer_sub $name => sub { ... }, \e%options; .Ve .PP 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. .PP 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. .PP Exported by default. .PP \fIOptions\fR .IX Subsection "Options" .PP A hashref of options can optionally be specified. .IP "package" 4 .IX Item "package" The package to generate the sub in. Will be overridden by a fully qualified \&\f(CW$name\fR option. If not specified, will default to the caller's package. .IP "attributes" 4 .IX Item "attributes" The \*(L"Subroutine Attributes\*(R" in perlsub to apply to the sub generated. Should be specified as an array reference. .SS "undefer_sub" .IX Subsection "undefer_sub" .Vb 1 \& my $coderef = undefer_sub \e&Foo::name; .Ve .PP If the passed coderef has been deferred this will \*(L"undefer\*(R" it. If the passed coderef has not been deferred, this will just return it. .PP If this is confusing, take a look at the example in the \*(L"\s-1SYNOPSIS\*(R"\s0. .PP Exported by default. .SS "defer_info" .IX Subsection "defer_info" .Vb 2 \& my $data = defer_info $sub; \& my ($name, $generator, $options, $undeferred_sub) = @$data; .Ve .PP Returns original arguments to defer_sub, plus the undeferred version if this sub has already been undeferred. .PP Note that \f(CW$sub\fR can be either the original deferred version or the undeferred version for convenience. .PP Not exported by default. .SS "undefer_all" .IX Subsection "undefer_all" .Vb 1 \& undefer_all(); .Ve .PP 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. .PP Exported by default. .SS "undefer_package" .IX Subsection "undefer_package" .Vb 1 \& undefer_package($package); .Ve .PP This undefers all deferred subs in a package. .PP Not exported by default. .SH "SUPPORT" .IX Header "SUPPORT" See Sub::Quote for support and contact information. .SH "AUTHORS" .IX Header "AUTHORS" See Sub::Quote for authors. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" See Sub::Quote for the copyright and license.