Commit | Line | Data |
eae82931 |
1 | package Sub::Defer; |
2 | |
3 | use strictures 1; |
4 | use base qw(Exporter); |
b1eebd55 |
5 | use Moo::_Utils; |
eae82931 |
6 | |
013a2be3 |
7 | our $VERSION = '1.003000'; |
8 | $VERSION = eval $VERSION; |
9 | |
a165a07f |
10 | our @EXPORT = qw(defer_sub undefer_sub); |
eae82931 |
11 | |
12 | our %DEFERRED; |
13 | |
a165a07f |
14 | sub undefer_sub { |
eae82931 |
15 | my ($deferred) = @_; |
16 | my ($target, $maker, $undeferred_ref) = @{ |
17 | $DEFERRED{$deferred}||return $deferred |
18 | }; |
9e858450 |
19 | return ${$undeferred_ref} |
20 | if ${$undeferred_ref}; |
eae82931 |
21 | ${$undeferred_ref} = my $made = $maker->(); |
ba37527b |
22 | |
23 | # make sure the method slot has not changed since deferral time |
24 | if (defined($target) && $deferred eq *{_getglob($target)}{CODE}||'') { |
a165a07f |
25 | no warnings 'redefine'; |
575ba24c |
26 | |
27 | # I believe $maker already evals with the right package/name, so that |
28 | # _install_coderef calls are not necessary --ribasushi |
a165a07f |
29 | *{_getglob($target)} = $made; |
30 | } |
9e858450 |
31 | $DEFERRED{$made} = $DEFERRED{$deferred}; |
ba37527b |
32 | |
eae82931 |
33 | return $made; |
34 | } |
35 | |
c4570291 |
36 | sub defer_info { |
37 | my ($deferred) = @_; |
38 | $DEFERRED{$deferred||''}; |
39 | } |
40 | |
a165a07f |
41 | sub defer_sub { |
eae82931 |
42 | my ($target, $maker) = @_; |
43 | my $undeferred; |
44 | my $deferred_string; |
9187b862 |
45 | my $deferred = sub { |
a165a07f |
46 | goto &{$undeferred ||= undefer_sub($deferred_string)}; |
9187b862 |
47 | }; |
eae82931 |
48 | $deferred_string = "$deferred"; |
9e858450 |
49 | $DEFERRED{$deferred} = [ $target, $maker, \$undeferred, $deferred ]; |
5cadba52 |
50 | _install_coderef($target => $deferred) if defined $target; |
eae82931 |
51 | return $deferred; |
52 | } |
53 | |
54 | 1; |
8213644a |
55 | |
0b6e5fff |
56 | =head1 NAME |
57 | |
58 | Sub::Defer - defer generation of subroutines until they are first called |
59 | |
8213644a |
60 | =head1 SYNOPSIS |
61 | |
62 | use Sub::Defer; |
63 | |
64 | my $deferred = defer_sub 'Logger::time_since_first_log' => sub { |
65 | my $t = time; |
66 | sub { time - $t }; |
67 | }; |
68 | |
0b6e5fff |
69 | Logger->time_since_first_log; # returns 0 and replaces itself |
70 | Logger->time_since_first_log; # returns time - $t |
8213644a |
71 | |
72 | =head1 DESCRIPTION |
73 | |
0b6e5fff |
74 | These subroutines provide the user with a convenient way to defer creation of |
8213644a |
75 | subroutines and methods until they are first called. |
76 | |
77 | =head1 SUBROUTINES |
78 | |
79 | =head2 defer_sub |
80 | |
81 | my $coderef = defer_sub $name => sub { ... }; |
82 | |
0b6e5fff |
83 | This subroutine returns a coderef that encapsulates the provided sub - when |
84 | it is first called, the provided sub is called and is -itself- expected to |
85 | return a subroutine which will be goto'ed to on subsequent calls. |
8213644a |
86 | |
0b6e5fff |
87 | If a name is provided, this also installs the sub as that name - and when |
88 | the subroutine is undeferred will re-install the final version for speed. |
8213644a |
89 | |
90 | =head2 undefer_sub |
91 | |
92 | my $coderef = undefer_sub \&Foo::name; |
93 | |
94 | If the passed coderef has been L<deferred|/defer_sub> this will "undefer" it. |
95 | If the passed coderef has not been deferred, this will just return it. |
96 | |
97 | If this is confusing, take a look at the example in the L</SYNOPSIS>. |
072d158f |
98 | |
99 | =head1 SUPPORT |
100 | |
1108b2e2 |
101 | See L<Moo> for support and contact information. |
072d158f |
102 | |
103 | =head1 AUTHORS |
104 | |
105 | See L<Moo> for authors. |
106 | |
107 | =head1 COPYRIGHT AND LICENSE |
108 | |
109 | See L<Moo> for the copyright and license. |