add CLONE method to fix Sub::Defer/Quote in threads
[gitmo/Moo.git] / lib / Sub / Defer.pm
CommitLineData
eae82931 1package Sub::Defer;
2
3use strictures 1;
4use base qw(Exporter);
b1eebd55 5use Moo::_Utils;
eae82931 6
013a2be3 7our $VERSION = '1.003000';
8$VERSION = eval $VERSION;
9
a165a07f 10our @EXPORT = qw(defer_sub undefer_sub);
eae82931 11
12our %DEFERRED;
13
a165a07f 14sub 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 36sub defer_info {
37 my ($deferred) = @_;
38 $DEFERRED{$deferred||''};
39}
40
a165a07f 41sub defer_sub {
eae82931 42 my ($target, $maker) = @_;
43 my $undeferred;
efdff87e 44 my $deferred;
45 $deferred = sub {
46 $undeferred ||= undefer_sub($deferred);
47 goto &$undeferred;
9187b862 48 };
9e858450 49 $DEFERRED{$deferred} = [ $target, $maker, \$undeferred, $deferred ];
5cadba52 50 _install_coderef($target => $deferred) if defined $target;
eae82931 51 return $deferred;
52}
53
efdff87e 54sub CLONE {
55 %DEFERRED = map { $_->[3] => $_ } values %DEFERRED;
56}
57
eae82931 581;
8213644a 59
0b6e5fff 60=head1 NAME
61
62Sub::Defer - defer generation of subroutines until they are first called
63
8213644a 64=head1 SYNOPSIS
65
66 use Sub::Defer;
67
68 my $deferred = defer_sub 'Logger::time_since_first_log' => sub {
69 my $t = time;
70 sub { time - $t };
71 };
72
0b6e5fff 73 Logger->time_since_first_log; # returns 0 and replaces itself
74 Logger->time_since_first_log; # returns time - $t
8213644a 75
76=head1 DESCRIPTION
77
0b6e5fff 78These subroutines provide the user with a convenient way to defer creation of
8213644a 79subroutines and methods until they are first called.
80
81=head1 SUBROUTINES
82
83=head2 defer_sub
84
85 my $coderef = defer_sub $name => sub { ... };
86
0b6e5fff 87This subroutine returns a coderef that encapsulates the provided sub - when
88it is first called, the provided sub is called and is -itself- expected to
89return a subroutine which will be goto'ed to on subsequent calls.
8213644a 90
0b6e5fff 91If a name is provided, this also installs the sub as that name - and when
92the subroutine is undeferred will re-install the final version for speed.
8213644a 93
94=head2 undefer_sub
95
96 my $coderef = undefer_sub \&Foo::name;
97
98If the passed coderef has been L<deferred|/defer_sub> this will "undefer" it.
99If the passed coderef has not been deferred, this will just return it.
100
101If this is confusing, take a look at the example in the L</SYNOPSIS>.
072d158f 102
103=head1 SUPPORT
104
1108b2e2 105See L<Moo> for support and contact information.
072d158f 106
107=head1 AUTHORS
108
109See L<Moo> for authors.
110
111=head1 COPYRIGHT AND LICENSE
112
113See L<Moo> for the copyright and license.