| 1 | 1 | ▶ | use 5.008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | package Class::Scaffold::ConstantImporter; | ||||||||||||||||||||
| 6 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | $Class::Scaffold::ConstantImporter::VERSION = '1.102280'; | |||||||||||||||||||
| 8 | } | ||||||||||||||||||||
| 9 | # ABSTRACT: Import environment constants as simple functions | ||||||||||||||||||||
| 10 | 1 | ▶ | use Devel::Caller qw(caller_args); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 0 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: not called CC=2 SCAR=13.2
| |||||||||||||||||||||
| 13 | 0 | shift; # we don't need the package name | |||||||||||||||||||
| 14 | 0 | my $callpkg = caller(0); | |||||||||||||||||||
| 15 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | # For each requested symbol, install a proxy sub into the caller's | ||||||||||||||||||||
| 18 | # namespace. When invoked, it will get the caller's $self and retrieve the | ||||||||||||||||||||
| 19 | # symbol's value from the caller's delegate. It will then replace itself | ||||||||||||||||||||
| 20 | # with a sub that just returns that value. | ||||||||||||||||||||
| 21 | # | ||||||||||||||||||||
| 22 | # The value is cached so that if the same constant is imported in | ||||||||||||||||||||
| 23 | # different packages we still only make one call to the delegate. | ||||||||||||||||||||
| 24 | # | ||||||||||||||||||||
| 25 | # That way the symbol can be used without the $self->delegate->... part. | ||||||||||||||||||||
| 26 | # You have to make sure that when the symbol is first used, you do it from | ||||||||||||||||||||
| 27 | # within a method whose $self has access to the delegate. | ||||||||||||||||||||
| 28 | # | ||||||||||||||||||||
| 29 | # The delegate is cached so that later this mechanism can be used even | ||||||||||||||||||||
| 30 | # from within subs that don't have access to the delegate. | ||||||||||||||||||||
| 31 | 0 | our %cache; | |||||||||||||||||||
| 32 | 0 | for my $symbol (@_) { | |||||||||||||||||||
| 33 | 0 | ▶ | *{"${callpkg}::${symbol}"} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=20.8
| |||||||||||||||||||||
| 34 | 0 | ▶ | unless (exists $cache{$symbol}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 0 | my $caller_self = (caller_args(1))[0]; | |||||||||||||||||||
| 36 | 0 | ▶ | our $delegate ||= $caller_self->delegate; | ||||||||||||||||||
|
Condition0%0/3
our $delegate ||= $caller_self->delegate
MC/DC0%0/2
our $delegate ||= $caller_self->delegate
our $delegate
$caller_self->delegate
Truth table0%0/3
A ||= B
A= our $delegateB= $caller_self->delegate
| |||||||||||||||||||||
| 37 | 0 | $cache{$symbol} = $delegate->$symbol; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 0 | ▶ | *{"${callpkg}::${symbol}"} = sub { $cache{$symbol} }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 41 | 0 | $cache{$symbol}; | |||||||||||||||||||
| 42 | 0 | }; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | 1 | 1; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | __END__ | ||||||||||||||||||||