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