blib/lib/Scalar/Defer.pm

filter: Statementstmt 80.6% 96 / 119 Branchbran 35.2% 12 / 34 Conditioncond 47.6% 10 / 21 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 89.6% 26 / 29 Podpod 75.0% 3 / 4 totaltotal 68.6% 149 / 217 CC 26 SCAR 17.0
CC
26
Cov
68%
BEGIN
17.5
DOES
5.7
VERSION
5.7
CRAP 5.5
↑ Summary
1 package Scalar::Defer;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our $VERSION = '0.23';
9 2 our @EXPORT = qw( lazy defer force );
10 2 our @EXPORT_OK = qw( is_deferred );
11 }
12  
13 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Exporter::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Class::InsideOut qw( register id );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use constant DEFER_PACKAGE => '0'; # This may change soon
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 my %_defer;
20  
21 4 ▶ sub defer (&) {
Subroutine defer: called CC=1 SCAR=0.0
Pod documented
22 4 my $cv = shift;
23 4 my $obj = register( bless(\(my $id) => __PACKAGE__) );
24 4 $_defer{ $id = id $obj } = $cv;
25 4 bless($obj => DEFER_PACKAGE);
26 }
27  
28 2 ▶ sub lazy (&) {
Subroutine lazy: called CC=1 SCAR=0.0
Pod documented
29 2 my $cv = shift;
30 2 my $obj = register( bless(\(my $id) => __PACKAGE__) );
31  
32 2 my ($value, $forced);
33 7 ▶ $_defer{ $id = id $obj } = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
34 7 ▶ $forced ? $value : scalar(++$forced, $value = &$cv)
Branch: 1 branch
BranchTrueFalseTotal
$forced ? : 5 2 7
35 2 };
36  
37 2 bless $obj => DEFER_PACKAGE;
38 }
39  
40 6 ▶ sub DEMOLISH {
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
41 6 delete $_defer{ id $_[0] };
42 }
43  
44 21 ▶ sub is_deferred ($) {
Subroutine is_deferred: called CC=1 SCAR=0.0
Pod documented
45 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 21 ref $_[0] eq DEFER_PACKAGE;
47 }
48  
49 2 ▶ use constant SUB_FORCE => sub ($) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=5 SCAR=16.2
50 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 &{
52 31 $_defer{ id $_[0] } ||= $_defer{do {
53 #
54 # The memory address was dislocated. Fortunately, its original
55 # refaddr is saved directly inside the scalar referent slot.
56 #
57 # So we remove the overload by blessing into UNIVERSAL, get the
58 # original refaddr back, and register it with ||= above to avoid
59 # doing the same thing next time. (Afterwards we rebless it back.)
60 #
61 # This of course assumes that nobody overloads ${} for UNIVERSAL
62 # (which will naturally break all objects using scalar-ref layout);
63 # if someone does, that someone is more crazy than we are and should
64 # be able to handle the consequences.
65 #
66 7 my $self = $_[0];
67 7 ▶ ref($self) eq DEFER_PACKAGE or return $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self eq 0 or return $self 6 1 7
68  
69 6 bless($self => 'UNIVERSAL');
70 6 my $id = $$self;
71 6 bless($self => DEFER_PACKAGE);
72 6 $id;
73 31 ▶ }} or do {
Condition83%5/6
($_defer{builtin::refaddr($_[0])} ||= $_defer{do { my $self = $_[0]; ref $self eq '0' or return $self; bless $self, 'UNIVERSAL'; my $id = $$self; bless $self, '0'; $id }}) || do { return 0 if caller eq 'Class::InsideOut'; die sprintf('Cannot locate thunk for memory address: 0x%X', builtin::refaddr($_[0])) }
l!l&&r!l&&!r
2406
$_defer{builtin::refaddr($_[0])} ||= $_defer{do { my $self = $_[0]; ref $self eq 0 or return $self; bless $self, "UNIVERSAL"; my $id = $$self; bless $self, 0; $id }}
l!l&&r!l&&!r
2416
MC/DC0%0/2
($_defer{builtin::refaddr($_[0])} ||= $_defer{do { my $self = $_[0]; ref $self eq '0' or return $self; bless $self, 'UNIVERSAL'; my $id = $$self; bless $self, '0'; $id }}) || do { return 0 if caller eq 'Class::InsideOut'; die sprintf('Cannot locate thunk for memory address: 0x%X', builtin::refaddr($_[0])) }
($_defer{builtin::refaddr($_[0])} ||= $_defer{do { my $self = $_[0]; ref $self eq '0' or return $self; bless $self, 'UNIVERSAL'; my $id = $$self; bless $self, '0'; $id }}) do { return 0 if caller eq 'Class::InsideOut'; die sprintf('Cannot locate thunk for memory address: 0x%X', builtin::refaddr($_[0])) }
MC/DC100%2/2
$_defer{builtin::refaddr($_[0])} ||= $_defer{do { my $self = $_[0]; ref $self eq 0 or return $self; bless $self, "UNIVERSAL"; my $id = $$self; bless $self, 0; $id }}
$_defer{builtin::refaddr($_[0])} $_defer{do { my $self = $_[0]; ref $self eq 0 or return $self; bless $self, "UNIVERSAL"; my $id = $$self; bless $self, 0; $id }}
Truth table33%1/3
A || B
A= ($_defer{builtin::refaddr($_[0])} ||= $_defer{do { my $self = $_[0]; ref $self eq '0' or return $self; bless $self, 'UNIVERSAL'; my $id = $$self; bless $self, '0'; $id }})B= do { return 0 if caller eq 'Class::InsideOut'; die sprintf('Cannot locate thunk for memory address: 0x%X', builtin::refaddr($_[0])) }
ABresult
1X1
011
000
Truth table100%3/3
A ||= B
A= $_defer{builtin::refaddr($_[0])}B= $_defer{do { my $self = $_[0]; ref $self eq 0 or return $self; bless $self, "UNIVERSAL"; my $id = $$self; bless $self, 0; $id }}
ABresult
1X1
011
000
74 6 ▶ return 0 if caller eq 'Class::InsideOut';
Branch: 1 branch
BranchTrueFalseTotal
if caller eq "Class::InsideOut" 6 0 6
75 0 die sprintf("Cannot locate thunk for memory address: 0x%X", id $_[0]);
76 };
77 };
78 2 };
79  
80 2 *force = SUB_FORCE();
81 }
82  
83 2 ▶BEGIN {
Subroutine BEGIN: called CC=8 SCAR=28.6
84 package Scalar::Defer::Deferred;
85 2 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
86 fallback => 1, map {
87 2 $_ => Scalar::Defer::SUB_FORCE(),
88 } qw( bool "" 0+ ${} @{} %{} &{} *{} )
89 2 );
90  
91 1 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
92 1 my $meth = our $AUTOLOAD;
93 1 my $idx = index($meth, '::');
94  
95 1 ▶ if ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= 0) 1 0 1
96 1 $meth = substr($meth, $idx + 2);
97 }
98  
99 1 unshift @_, Scalar::Defer::SUB_FORCE()->(shift(@_));
100 1 goto &{$_[0]->can($meth)};
101 };
102  
103 {
104 2 foreach my $sym (grep {
105 12 ▶ $_ ne 'DESTROY' and $_ ne 'DEMOLISH' and $_ ne 'BEGIN'
Condition33%5/15
$_ ne 'DESTROY' && $_ ne 'DEMOLISH' && $_ ne 'BEGIN' && $_ ne 'END' && $_ ne 'AUTOLOAD' && $_ ne 'CLONE_SKIP'
!ll&&!rl&&r
0012
$_ ne "DESTROY" and $_ ne "DEMOLISH" and $_ ne "BEGIN" and $_ ne "END" and $_ ne "AUTOLOAD"
!ll&&!rl&&r
0012
$_ ne "DESTROY" and $_ ne "DEMOLISH" and $_ ne "BEGIN" and $_ ne "END"
!ll&&!rl&&r
0012
$_ ne "DESTROY" and $_ ne "DEMOLISH" and $_ ne "BEGIN"
!ll&&!rl&&r
0012
$_ ne "DESTROY" and $_ ne "DEMOLISH"
!ll&&!rl&&r
0012
MC/DC0%0/6
$_ ne 'DESTROY' && $_ ne 'DEMOLISH' && $_ ne 'BEGIN' && $_ ne 'END' && $_ ne 'AUTOLOAD' && $_ ne 'CLONE_SKIP'
$_ ne "DESTROY" $_ ne "DEMOLISH" $_ ne "BEGIN" $_ ne "END" $_ ne "AUTOLOAD" $_ ne 'CLONE_SKIP'
Truth table14%1/7
A and B and C and D and E && F
A= $_ ne "DESTROY"B= $_ ne "DEMOLISH"C= $_ ne "BEGIN"D= $_ ne "END"E= $_ ne "AUTOLOAD"F= $_ ne 'CLONE_SKIP'
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
106 and $_ ne 'END' and $_ ne 'AUTOLOAD' and $_ ne 'CLONE_SKIP'
107 } keys %UNIVERSAL::) {
108 12 my $code = q[
109 sub $sym {
110 if ( defined Scalar::Util::blessed($_[0]) ) {
111 unshift @_, Scalar::Defer::SUB_FORCE()->(shift(@_));
112 goto &{$_[0]->can("$sym")};
113 }
114 else {
115 # Protect against future ALLCAPS methods
116 return if $_[0] eq Scalar::Defer::DEFER_PACKAGE;
117  
118 return shift->SUPER::$sym(@_);
119 }
120 }
121 ];
122  
123 12 $code =~ s/\$sym/$sym/g;
124  
125 12 local $@;
126 0 ▶ eval $code;
Subroutine DOES: not called CC=3 SCAR=17.5
Subroutine VERSION: called CC=3 SCAR=17.5
Subroutine can: called CC=3 SCAR=17.5
Subroutine import: not called CC=3 SCAR=17.5
Subroutine isa: called CC=3 SCAR=17.5
Subroutine unimport: not called CC=3 SCAR=17.5
Branch: 12 branches
BranchTrueFalseTotal
if (defined builtin::blessed($_[0])) { } 0 0 0
if $_[0] eq 0 0 0 0
if (defined builtin::blessed($_[0])) { } 1 0 1
if $_[0] eq 0 0 0 0
if (defined builtin::blessed($_[0])) { } 4 1 5
if $_[0] eq 0 0 1 1
if (defined builtin::blessed($_[0])) { } 0 0 0
if $_[0] eq 0 0 0 0
if (defined builtin::blessed($_[0])) { } 1 0 1
if $_[0] eq 0 0 0 0
if (defined builtin::blessed($_[0])) { } 0 0 0
if $_[0] eq 0 0 0 0
127 12 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
128 }
129  
130 2 *DESTROY = \&Scalar::Defer::DESTROY;
131 2 *DEMOLISH = \&Scalar::Defer::DEMOLISH;
132 }
133 }
134  
135 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
136 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
137 2 @{DEFER_PACKAGE().'::ISA'} = ('Scalar::Defer::Deferred');
138 }
139  
140 2 1;
141  
142 __END__
↑ Summary