Sub-Become

 view release on metacpan or  search on metacpan

lib/Sub/Become.pm  view on Meta::CPAN

Or exploit the fact that C<become> returns the code reference for the
new definition:

    sub expensive {
        my $thing = some_expensive_calculation();
        return (become { $thing })->();
    }

=cut

sub become(&) {
    croak "become needs a coderef"
      unless @_ == 1 && 'CODE' eq ref $_[0];
    no strict 'refs';
    no warnings 'redefine';
    return *{ ( caller 1 )[3] } = shift;
}

1;
__END__



( run in 0.998 second using v1.01-cache-2.11-cpan-49f99fa48dc )