blib/lib/Safe/Hole.pm

filter: Statementstmt 98.9% 94 / 95 Branchbran 92.1% 35 / 38 Conditioncond 84.6% 11 / 13 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 95.4% 166 / 174 CC 26 SCAR 20.0
CC
26
Cov
96%
wrap
15.1
call
6.0
new
4.0
CRAP 7.4
↑ Summary Hole.xs »
1 # Safe::Hole - make a hole to the original main compartment in the Safe compartment
2 # Copyright 1999-2001, Sey Nakajima, All rights reserved.
3 # This program is free software under the GPL.
4 package Safe::Hole;
5  
6 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.17';
12  
13 4 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 XSLoader::load( 'Safe::Hole', $VERSION );
15  
16 9 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
17 9 my ( $class, $args ) = @_;
18 9 my $self = bless {}, $class;
19 9 ▶ $args = { ROOT => $args || 'main' } unless ref $args eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 2 7 9
Condition100%2/2
$args || 'main'
l!l
11
MC/DC100%1/1
$args || 'main'
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
20 9 ▶ if ( $args->{ROOT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'ROOT'}) { } 2 7 9
21 2 $self->{PACKAGE} = $args->{ROOT};
22 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 $self->{STASH} = \%{"$args->{ROOT}::"};
24 }
25 else {
26 7 $self->{INC} = [ \%INC, \@INC ];
27 7 $self->{OPMASK} = _get_current_opmask();
28 7 $self->{PACKAGE} = 'main';
29 7 $self->{STASH} = \%main::;
30 }
31 9 $self;
32 }
33  
34 59 ▶sub call {
Subroutine call: called CC=6 SCAR=17.9
Pod documented
35 59 my $self = shift;
36 59 my $coderef = shift;
37 59 my @args = @_;
38  
39 # _hole_call_sv() does not seem to like being ripped off the stack
40 # so we need some fancy footwork to catch and re-throw the error
41  
42 59 my ( @r, $did_not_die );
43 59 my $wantarray = wantarray;
44  
45 local (*INC), do {
46 55 *INC = $_ for @{ $self->{INC} };
47 59 ▶ } if $self->{INC};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"INC"} 55 4 59
48  
49 # Safe::Hole::User contains nothing but is a placeholder so that
50 # things that are called via Safe::Hole can Carp::croak properly.
51  
52 package Safe::Hole::User; # Package name on a different line to keep it from being indexed
53  
54 59 ▶ my $inner_call = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
55 59 my $prev_error = $@;
56 59 eval {
57 59 @_ = @args;
58 59 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 6 53 59
59 6 @r = &$coderef;
60 }
61 else {
62 53 @r = scalar &$coderef;
63 }
64 56 $did_not_die = 1;
65 };
66 # Restore $@ if the eval succeeded, so that a successful eval
67 # during DESTROY doesn't clobber an in-flight exception (GH#1).
68 59 ▶ $@ = $prev_error if $did_not_die;
Branch: 1 branch
BranchTrueFalseTotal
if $did_not_die 56 3 59
69 59 };
70  
71 59 ▶ Safe::Hole::_hole_call_sv( $self->{STASH}, ${ $self->{OPMASK} || \undef }, $inner_call );
Condition100%2/2
$self->{'OPMASK'} || \(undef)
l!l
554
MC/DC100%1/1
$self->{'OPMASK'} || \(undef)
$self->{'OPMASK'}
Truth table100%2/2
A
A= $self->{'OPMASK'}
Aresult
11
00
72  
73 59 ▶ die $@ unless $did_not_die;
Branch: 1 branch
BranchTrueFalseTotal
unless $did_not_die 3 56 59
74 56 ▶ return wantarray ? @r : $r[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 50 56
75 }
76  
77 12 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod documented
78 12 my $self = shift;
79 12 $self->{PACKAGE};
80 }
81  
82 25 ▶sub wrap {
Subroutine wrap: called CC=15 SCAR=27.2
Pod documented
83 25 my ( $self, $ref, $cpt, $name ) = @_;
84 25 my ( $result, $typechar, $word );
85 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 25 ▶ if ( $cpt && $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cpt and $name) 18 7 25
Condition66%2/3
$cpt and $name
!ll&&!rl&&r
7018
MC/DC50%1/2
$cpt and $name
$cpt $name
Truth table66%2/3
A and B
A= $cptB= $name
ABresult
0X0
100
111
87 18 ▶ croak "Safe object required" unless ref($cpt) eq 'Safe';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cpt eq "Safe" 0 18 18
88 18 ▶ if ( $name =~ /^(\W)(\w+(::\w+)*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(\W)(\w+(::\w+)*)$/u) { } 17 1 18
89 17 ( $typechar, $word ) = ( $1, $2 );
90 }
91 else {
92 1 croak "'$name' not a valid name";
93 }
94 }
95 24 my $type = ref $ref;
96 24 ▶ if ( $type eq '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq '') { } 1 23 24
elsif ($type eq 'CODE') { } 11 12 23
elsif (%{$type . '::';}) { } 12 0 12
97 1 croak "reference required";
98 }
99 elsif ( $type eq 'CODE' ) {
100 11 ▶ $result = sub { $self->call( $ref, @_ ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
101 11 ▶ if ( defined $typechar && $typechar eq '&' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $typechar and $typechar eq '&') { } 10 1 11
elsif ($typechar) { } 1 0 1
Condition66%2/3
defined $typechar and $typechar eq "&"
!ll&&!rl&&r
0110
MC/DC50%1/2
defined $typechar and $typechar eq "&"
defined $typechar $typechar eq "&"
Truth table66%2/3
A and B
A= defined $typecharB= $typechar eq "&"
ABresult
0X0
100
111
102 10 *{ $cpt->root() . "::$word" } = $result;
103 }
104 elsif ($typechar) {
105 1 croak "'$name' type mismatch with $type";
106 }
107 }
108 12 elsif ( %{ $type . '::' } ) {
109 12 my $wrapclass = ref($self) . '::' . $self->root() . '::' . $type;
110 5 ▶ *{ $wrapclass . '::AUTOLOAD' } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 $self->call(
112 35 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
113 4 ▶ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 35 my $self = shift;
115 35 ▶ return if $AUTOLOAD =~ /::DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $AUTOLOAD =~ /::DESTROY$/u 12 23 35
116 23 my $name = $AUTOLOAD;
117 23 $name =~ s/.*://;
118 23 $self->{OBJ}->$name(@_);
119 },
120 @_
121 35 );
122 }
123 12 ▶ unless defined &{ $wrapclass . '::AUTOLOAD' };
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{$wrapclass . '::AUTOLOAD';} 5 7 12
124 12 $result = bless { OBJ => $ref }, $wrapclass;
125 12 ▶ if ( defined $typechar && $typechar eq '$' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $typechar and $typechar eq '$') { } 5 7 12
elsif ($typechar) { } 1 6 7
Condition100%3/3
defined $typechar and $typechar eq "\$"
!ll&&!rl&&r
615
MC/DC100%2/2
defined $typechar and $typechar eq "\$"
defined $typechar $typechar eq "\$"
Truth table100%3/3
A and B
A= defined $typecharB= $typechar eq "\$"
ABresult
0X0
100
111
126 5 ${ $cpt->varglob($word) } = $result;
127 }
128 elsif ($typechar) {
129 1 croak "'$name' type mismatch with object (must be scalar)";
130 }
131 }
132 else {
133 0 croak "type '$type' is not supported";
134 }
135 21 $result;
136 }
137  
138 # Autoload methods go after =cut, and are processed by the autosplit program.
139  
140 4 1;
141 __END__
↑ Summary Hole.xs »