| 1 | package Text::APL::WithCaching; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use base 'Text::APL::Core'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | sub _process { | ||||||||||
| 9 | 3 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_process: called CC=3 SCAR=11.0
| |||||||||||
| 10 | 3 | my ($input, $context, $cb) = @_; | |||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | if (my $cache = $self->_load_cache($context)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 13 | 2 | ▶ | if ($cache->{id} ne $context->id) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | $cache->{sub_ref} = | ||||||||||
| 15 | 1 | $self->SUPER::_compile($cache->{code}, $context); | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | 2 | return $cb->($self, $cache->{sub_ref}); | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 1 | $self->SUPER::_process(@_); | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | sub _compile { | ||||||||||
| 25 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_compile: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | my ($code, $context) = @_; | |||||||||
| 27 | |||||||||||
| 28 | 1 | my $sub_ref = $self->SUPER::_compile($code, $context); | |||||||||
| 29 | |||||||||||
| 30 | 1 | $self->_cache($code, $context, $sub_ref); | |||||||||
| 31 | |||||||||||
| 32 | 1 | return $sub_ref; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | sub _load_cache { | ||||||||||
| 36 | 3 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_load_cache: called CC=3 SCAR=11.0
| |||||||||||
| 37 | 3 | my ($context) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 3 | ▶ | return unless defined $context->name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | 3 | ▶ | return unless exists $self->{cache}->{$context->name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | 2 | return $self->{cache}->{$context->name}; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub _cache { | ||||||||||
| 47 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_cache: called CC=2 SCAR=7.0
| |||||||||||
| 48 | 1 | my ($code, $context, $sub_ref) = @_; | |||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | return unless defined $context->name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 1 | $self->{cache}->{$context->name} = { | |||||||||
| 53 | id => $context->id, | ||||||||||
| 54 | code => $code, | ||||||||||
| 55 | sub_ref => $sub_ref | ||||||||||
| 56 | }; | ||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 2 | 1; | |||||||||
| 60 | __END__ | ||||||||||