| 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
| |||||||||||||||||||||||
| 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|::)/
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}
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | # Find !Devel::Spy | ||||||||||||||||||||||
| 62 | 0 | ▶ | unless ( $pkg =~ $class_rx ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||