Spy/Util.pm

filter: Statementstmt 36.3% 20 / 55 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 6 / 12 Podpod 100% 4 / 4 totaltotal 33.3% 30 / 90 CC 10 SCAR 28.3
CC
10
Cov
27%
wrap_thing
42.0
comes_from
20.0
compile_this
6.0
CRAP 17.0
↑ Summary Spy/TieArray.pm »
1 package Devel::Spy::Util;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶sub Y { ## no critic (Prototype)
Subroutine Y: not called CC=1 SCAR=6.9
Pod documented
11 # The Y combinator.
12 0 my ( undef, $curried_rec ) = @_;
13 0 ▶ my $p = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 0 my $f = shift @_;
15 0 ▶ return $curried_rec->( sub { $f->($f)->(@_) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 0 };
17 0 return $p->($p);
18 }
19  
20 0 ▶sub compile_this {
Subroutine compile_this: not called CC=2 SCAR=17.9
Pod documented
21  
22 # Accepts some source code and expects to return a true
23 # value. Devel::Spy::_obj uses this to compile a bunch of subs but
24 # without having to repeat the "eval or croak" stuff all over the
25 # place.
26 #
27 # Example:
28 # my $sub = Devel::Spy::Util::compile_this( <<"SRC" );
29 # sub ... {
30 # ...
31 # };
32 # 1;
33 # SRC
34 0 my ( undef, $src ) = @_;
35 0 my ( $package, $filename, $line ) = caller;
36  
37 # Add some sugar to make the code appear in the proper location.
38 0 $src = <<"CODE";
39 0 #line @{[$line]} "@{[$filename]}"
40 package $package;
41 $src
42 CODE
43  
44 ## no critic (Eval)
45 0 ▶ my $result = eval $src
Branch: 1 branch
BranchTrueFalseTotal
my $result = eval $src or Carp::confess("$@ while compiling:\n$src") 0 0 0
46 or Carp::confess "$@ while compiling:\n$src";
47 0 return $result;
48 }
49  
50 1 my %class_rx_cache;
51  
52 0 ▶sub comes_from {
Subroutine comes_from: not called CC=4 SCAR=30.0
Pod documented
53 0 my $class = shift @_;
54 0 ▶ my $class_rx = $class_rx_cache{$class} ||= qr/\A\Q$class\E(?:\z|::)/;
Condition0%0/2
$class_rx_cache{$class} ||= qr/\A\Q$class\E(?:\z|::)/
l!l
00
MC/DC0%0/1
$class_rx_cache{$class} ||= qr/\A\Q$class\E(?:\z|::)/
$class_rx_cache{$class}
Truth table0%0/2
A
A= $class_rx_cache{$class}
Aresult
11
00
55  
56 # Returns a string showing the location of the non-Devel::Spy code
57 # that's higher in the call stack.
58 0 my $cx = 1;
59 0 ▶ while ( my ( $pkg, undef, $line ) = caller $cx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($pkg, undef, $line) = caller $cx++) 0 0 0
60  
61 # Find !Devel::Spy
62 0 ▶ unless ( $pkg =~ $class_rx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg =~ /$class_rx/) 0 0 0
63 0 return "($pkg:$line)";
64 }
65 }
66  
67 # Huh? I suppose this only occurs if Devel::Spy is the *only*
68 # thing in the call stack and I'm not even sure how that happens.
69 0 return;
70 }
71  
72 0 ▶sub wrap_thing {
Subroutine wrap_thing: not called CC=6 SCAR=37.4
Pod documented
73 0 my ( $class, $thing, $code ) = @_;
74  
75 # Use a tied proxy to $thing instead of $thing directly. But only
76 # if $thing is a reference.
77 0 my $reftype = Scalar::Util::reftype $thing;
78 0 ▶ return $thing unless defined $reftype;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reftype 0 0 0
79  
80 # This may be a really bad idea.
81 0 $class =~ s/::Util\z//;
82  
83 # Return a tied wrapper over $thing.
84 0 ▶ if ( 'HASH' eq $reftype ) {
Branch: 4 branches
BranchTrueFalseTotal
if ('HASH' eq $reftype) { } 0 0 0
elsif ('ARRAY' eq $reftype) { } 0 0 0
elsif ($reftype =~ /^(?:SCALAR|REF|CODE|LVALUE|REGEXP|VSTRING|BIND)\z/) { } 0 0 0
elsif ($reftype =~ /^(?:GLOB|FORMAT|IO)\z/) { } 0 0 0
85 0 tie my %pretend_self, "$class\::TieHash", $thing, $code;
86 0 return \%pretend_self;
87 }
88 elsif ( 'ARRAY' eq $reftype ) {
89 0 tie my @pretend_self, "$class\::TieArray", $thing, $code;
90 0 return \@pretend_self;
91 }
92 elsif ( $reftype =~ /^(?:SCALAR|REF|CODE|LVALUE|REGEXP|VSTRING|BIND)\z/ ) {
93 0 tie my $pretend_self, "$class\::TieScalar", $thing, $code;
94 0 return \$pretend_self;
95 }
96 elsif ( $reftype =~ /^(?:GLOB|FORMAT|IO)\z/ ) {
97 0 my $pretend_self = Symbol::gensym();
98 0 tie *$pretend_self, "$class\::TieHandle", $thing, $code;
99 0 return $pretend_self;
100 }
101  
102 # Missing implementations?
103 0 Carp::croak "Unsupported reftype: $reftype on "
104 . overload::StrVal($thing);
105 }
106  
107 1 1;
108  
109 __END__
↑ Summary Spy/TieArray.pm »