| 1 | package Concierge::Desk::DeferredComponent v0.13.0; | ||||||||||
| 2 | 1 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 1 | our $VERSION = 'v0.13.0'; | |||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: Stand-in that defers a 'defer' component's real construction until first use | ||||||||||
| 7 | |||||||||||
| 8 | 1 | our $AUTOLOAD; | |||||||||
| 9 | |||||||||||
| 10 | 8 | ▶ | sub new ($class, %args) { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 11 | return bless { | ||||||||||
| 12 | name => $args{name}, | ||||||||||
| 13 | class => $args{class}, | ||||||||||
| 14 | payload => $args{payload}, | ||||||||||
| 15 | 8 | _real => undef, # built on first use, cached here once successful | |||||||||
| 16 | }, $class; | ||||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 10 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=13.9
| |||||||||||
| 20 | 10 | my $self = shift; | |||||||||
| 21 | |||||||||||
| 22 | 10 | my $called = $AUTOLOAD; | |||||||||
| 23 | 10 | $called =~ s/.*:://; | |||||||||
| 24 | 10 | ▶ | return if $called eq 'DESTROY'; # never fall through to build-on-teardown | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | |||||||||||
| 26 | 10 | ▶ | unless ($self->{_real}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 5 | my $comp; | |||||||||
| 28 | 5 | my $ok = eval { | |||||||||
| 29 | 5 | (my $file = $self->{class}) =~ s{::}{/}g; | |||||||||
| 30 | 5 | require "$file.pm"; | |||||||||
| 31 | 5 | $comp = $self->{class}->new($self->{payload}); | |||||||||
| 32 | 3 | 1; | |||||||||
| 33 | }; | ||||||||||
| 34 | 5 | ▶ | if ($ok) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 3 | $self->{_real} = $comp; | |||||||||
| 36 | } else { | ||||||||||
| 37 | # Permanent-for-this-instance failure -- matches | ||||||||||
| 38 | # UnavailableComponent precedent exactly; no retry, ever. | ||||||||||
| 39 | 2 | require Concierge::Desk::UnavailableComponent; | |||||||||
| 40 | $self->{_real} = Concierge::Desk::UnavailableComponent->new( | ||||||||||
| 41 | 2 | name => $self->{name}, reason => $@, | |||||||||
| 42 | ); | ||||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 10 | return $self->{_real}->$called(@_); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # Explicit no-op DESTROY. Without this, object teardown would invoke | ||||||||||
| 50 | # AUTOLOAD for 'DESTROY' (Perl calls AUTOLOAD for a missing DESTROY just | ||||||||||
| 51 | # like any other missing method); the 'return if DESTROY' guard above | ||||||||||
| 52 | # only prevents AUTOLOAD from returning early in that case, it does not | ||||||||||
| 53 | # avoid the AUTOLOAD call itself -- and critically, without this, object | ||||||||||
| 54 | # teardown at the end of every test/request would itself trigger the | ||||||||||
| 55 | # deferred build. | ||||||||||
| 56 | 0 | ▶ | sub DESTROY { } | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||
| 57 | |||||||||||
| 58 | 1 | 1; | |||||||||
| 59 | |||||||||||
| 60 | __END__ | ||||||||||