$DEFERRED{$deferred}||return $deferred
};
${$undeferred_ref} = my $made = $maker->();
- if (defined($target)) {
+
+ # make sure the method slot has not changed since deferral time
+ if (defined($target) && $deferred eq *{_getglob($target)}{CODE}||'') {
no warnings 'redefine';
+
+ # I believe $maker already evals with the right package/name, so that
+ # _install_coderef calls are not necessary --ribasushi
*{_getglob($target)} = $made;
}
+ push @{$DEFERRED{$made} = $DEFERRED{$deferred}}, $made;
+
return $made;
}
+sub defer_info {
+ my ($deferred) = @_;
+ $DEFERRED{$deferred||''};
+}
+
sub defer_sub {
my ($target, $maker) = @_;
my $undeferred;
};
$deferred_string = "$deferred";
$DEFERRED{$deferred} = [ $target, $maker, \$undeferred ];
- *{_getglob $target} = $deferred if defined($target);
+ _install_coderef($target => $deferred) if defined $target;
return $deferred;
}
1;
-=pod
+=head1 NAME
+
+Sub::Defer - defer generation of subroutines until they are first called
=head1 SYNOPSIS
sub { time - $t };
};
-What the above does is set the Logger::time_since_first_log subroutine to be
-the codref that was passed to it, but then after it gets run once, it becomes
-the returned coderef.
+ Logger->time_since_first_log; # returns 0 and replaces itself
+ Logger->time_since_first_log; # returns time - $t
=head1 DESCRIPTION
-These subroutines provide the user with a convenient way to defer create of
+These subroutines provide the user with a convenient way to defer creation of
subroutines and methods until they are first called.
=head1 SUBROUTINES
my $coderef = defer_sub $name => sub { ... };
-RIBASUSHI FIX ME PLEASE!!!!
+This subroutine returns a coderef that encapsulates the provided sub - when
+it is first called, the provided sub is called and is -itself- expected to
+return a subroutine which will be goto'ed to on subsequent calls.
-Given name to install a subroutine into and a coderef that returns a coderef,
-this function will set up the subroutine such that when it is first called it
-will be replaced with the returned coderef.
+If a name is provided, this also installs the sub as that name - and when
+the subroutine is undeferred will re-install the final version for speed.
=head2 undefer_sub
If the passed coderef has not been deferred, this will just return it.
If this is confusing, take a look at the example in the L</SYNOPSIS>.
+
+=head1 SUPPORT
+
+See L<Moo> for support and contact informations.
+
+=head1 AUTHORS
+
+See L<Moo> for authors.
+
+=head1 COPYRIGHT AND LICENSE
+
+See L<Moo> for the copyright and license.