initial spike towards sub naming to collaborate with namespace checks in DBIC
[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
a165a07f 7our @EXPORT = qw(defer_sub undefer_sub);
eae82931 8
9our %DEFERRED;
10
a165a07f 11sub undefer_sub {
eae82931 12 my ($deferred) = @_;
13 my ($target, $maker, $undeferred_ref) = @{
14 $DEFERRED{$deferred}||return $deferred
15 };
16 ${$undeferred_ref} = my $made = $maker->();
ba37527b 17
18 # make sure the method slot has not changed since deferral time
19 if (defined($target) && $deferred eq *{_getglob($target)}{CODE}||'') {
a165a07f 20 no warnings 'redefine';
575ba24c 21
22 # I believe $maker already evals with the right package/name, so that
23 # _install_coderef calls are not necessary --ribasushi
a165a07f 24 *{_getglob($target)} = $made;
25 }
c4570291 26 push @{$DEFERRED{$made} = $DEFERRED{$deferred}}, $made;
ba37527b 27
eae82931 28 return $made;
29}
30
c4570291 31sub defer_info {
32 my ($deferred) = @_;
33 $DEFERRED{$deferred||''};
34}
35
a165a07f 36sub defer_sub {
eae82931 37 my ($target, $maker) = @_;
38 my $undeferred;
39 my $deferred_string;
9187b862 40 my $deferred = sub {
a165a07f 41 goto &{$undeferred ||= undefer_sub($deferred_string)};
9187b862 42 };
eae82931 43 $deferred_string = "$deferred";
44 $DEFERRED{$deferred} = [ $target, $maker, \$undeferred ];
575ba24c 45 _install_coderef $target => $deferred if defined $target;
eae82931 46 return $deferred;
47}
48
491;
8213644a 50
0b6e5fff 51=head1 NAME
52
53Sub::Defer - defer generation of subroutines until they are first called
54
8213644a 55=head1 SYNOPSIS
56
57 use Sub::Defer;
58
59 my $deferred = defer_sub 'Logger::time_since_first_log' => sub {
60 my $t = time;
61 sub { time - $t };
62 };
63
0b6e5fff 64 Logger->time_since_first_log; # returns 0 and replaces itself
65 Logger->time_since_first_log; # returns time - $t
8213644a 66
67=head1 DESCRIPTION
68
0b6e5fff 69These subroutines provide the user with a convenient way to defer creation of
8213644a 70subroutines and methods until they are first called.
71
72=head1 SUBROUTINES
73
74=head2 defer_sub
75
76 my $coderef = defer_sub $name => sub { ... };
77
0b6e5fff 78This subroutine returns a coderef that encapsulates the provided sub - when
79it is first called, the provided sub is called and is -itself- expected to
80return a subroutine which will be goto'ed to on subsequent calls.
8213644a 81
0b6e5fff 82If a name is provided, this also installs the sub as that name - and when
83the subroutine is undeferred will re-install the final version for speed.
8213644a 84
85=head2 undefer_sub
86
87 my $coderef = undefer_sub \&Foo::name;
88
89If the passed coderef has been L<deferred|/defer_sub> this will "undefer" it.
90If the passed coderef has not been deferred, this will just return it.
91
92If this is confusing, take a look at the example in the L</SYNOPSIS>.