1Sub::Defer(3)         User Contributed Perl Documentation        Sub::Defer(3)
2
3
4

NAME

6       Sub::Defer - Defer generation of subroutines until they are first
7       called
8

SYNOPSIS

10        use Sub::Defer;
11
12        my $deferred = defer_sub 'Logger::time_since_first_log' => sub {
13           my $t = time;
14           sub { time - $t };
15        };
16
17         Logger->time_since_first_log; # returns 0 and replaces itself
18         Logger->time_since_first_log; # returns time - $t
19

DESCRIPTION

21       These subroutines provide the user with a convenient way to defer
22       creation of subroutines and methods until they are first called.
23

SUBROUTINES

25   defer_sub
26        my $coderef = defer_sub $name => sub { ... }, \%options;
27
28       This subroutine returns a coderef that encapsulates the provided sub -
29       when it is first called, the provided sub is called and is -itself-
30       expected to return a subroutine which will be goto'ed to on subsequent
31       calls.
32
33       If a name is provided, this also installs the sub as that name - and
34       when the subroutine is undeferred will re-install the final version for
35       speed.
36
37       Exported by default.
38
39       Options
40
41       A hashref of options can optionally be specified.
42
43       package
44           The package to generate the sub in.  Will be overridden by a fully
45           qualified $name option.  If not specified, will default to the
46           caller's package.
47
48       attributes
49           The "Subroutine Attributes" in perlsub to apply to the sub
50           generated.  Should be specified as an array reference.
51
52   undefer_sub
53        my $coderef = undefer_sub \&Foo::name;
54
55       If the passed coderef has been deferred this will "undefer" it.  If the
56       passed coderef has not been deferred, this will just return it.
57
58       If this is confusing, take a look at the example in the "SYNOPSIS".
59
60       Exported by default.
61
62   defer_info
63        my $data = defer_info $sub;
64        my ($name, $generator, $options, $undeferred_sub) = @$data;
65
66       Returns original arguments to defer_sub, plus the undeferred version if
67       this sub has already been undeferred.
68
69       Note that $sub can be either the original deferred version or the
70       undeferred version for convenience.
71
72       Not exported by default.
73
74   undefer_all
75        undefer_all();
76
77       This will undefer all deferred subs in one go.  This can be very useful
78       in a forking environment where child processes would each have to
79       undefer the same subs.  By calling this just before you start forking
80       children you can undefer all currently deferred subs in the parent so
81       that the children do not have to do it.  Note this may bake the
82       behavior of some subs that were intended to calculate their behavior
83       later, so it shouldn't be used midway through a module load or class
84       definition.
85
86       Exported by default.
87
88   undefer_package
89         undefer_package($package);
90
91       This undefers all deferred subs in a package.
92
93       Not exported by default.
94

SUPPORT

96       See Sub::Quote for support and contact information.
97

AUTHORS

99       See Sub::Quote for authors.
100
102       See Sub::Quote for the copyright and license.
103
104
105
106perl v5.30.0                      2019-10-02                     Sub::Defer(3)
Impressum