| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | package PerlGuard::Agent::LexWrap; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Lexically scoped subroutine wrappers | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.26'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | { | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 16 | ▶ | *CORE::GLOBAL::caller = sub (;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=23.6
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 16 | ▶ | my ($height) = ($_[0]||0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[0] || 0
MC/DC100%1/1
$_[0] || 0
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 16 | my $i=1; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 16 | my $name_cache; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 16 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 17 | 40 | ▶ | my @caller = CORE::caller($i++) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 40 | ▶ | $caller[3] = $name_cache if $name_cache; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 40 | ▶ | $name_cache = $caller[0] eq 'PerlGuard::Agent::LexWrap' ? $caller[3] : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 40 | ▶ | next if $name_cache || $height-- != 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name_cache or $height-- != 0
MC/DC50%1/2
$name_cache or $height-- != 0
$name_cache
$height-- != 0
Truth table66%2/3
A or B
A= $name_cacheB= $height-- != 0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 16 | ▶ | return wantarray ? @_ ? @caller : @caller[0..2] : $caller[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub import { no strict 'refs'; *{caller()."::wrap"} = \&wrap } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub wrap (*@) { ## no critic Prototypes | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap: not called CC=11 SCAR=46.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my ($typeglob, %wrapper) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $typeglob = (ref $typeglob || $typeglob =~ /::/) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $typeglob or $typeglob =~ /::/
MC/DC0%0/2
ref $typeglob or $typeglob =~ /::/
ref $typeglob
$typeglob =~ /::/
Truth table0%0/3
A or B
A= ref $typeglobB= $typeglob =~ /::/
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | ? $typeglob | ||||||||||||||||||||||||||||||||||||||||||
| 32 | : caller()."::$typeglob"; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $original; | |||||||||||||||||||||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | $original = ref $typeglob eq 'CODE' && $typeglob | ||||||||||||||||||||||||||||||||||||||||||
| 37 | || *$typeglob{CODE} | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | || Carp::croak "Can't wrap non-existent subroutine ", $typeglob; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/7
ref $typeglob eq 'CODE' && $typeglob || *$typeglob{'CODE'} || Carp::croak(q[Can't wrap non-existent subroutine ], $typeglob)
ref $typeglob eq "CODE" and $typeglob or *$typeglob{"CODE"}
ref $typeglob eq "CODE" and $typeglob
MC/DC0%0/2
ref $typeglob eq 'CODE' && $typeglob || *$typeglob{'CODE'} || Carp::croak(q[Can't wrap non-existent subroutine ], $typeglob)
ref $typeglob eq "CODE"
$typeglob
Truth table0%0/3
A and B
A= ref $typeglob eq "CODE"B= $typeglob
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | Carp::croak "'$_' value is not a subroutine reference" | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | foreach grep {$wrapper{$_} && ref $wrapper{$_} ne 'CODE'} | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$wrapper{$_} && ref $wrapper{$_} ne 'CODE'
MC/DC0%0/2
$wrapper{$_} && ref $wrapper{$_} ne 'CODE'
$wrapper{$_}
ref $wrapper{$_} ne 'CODE'
Truth table0%0/3
A && B
A= $wrapper{$_}B= ref $wrapper{$_} ne 'CODE'
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | qw(pre post); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my ($caller, $unwrap) = *CORE::GLOBAL::caller{CODE}; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $imposter = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ($unwrap) { goto &$original } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | my ($return, $prereturn); | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $prereturn = $return = []; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | () = $wrapper{pre}->(\@_,$return) if $wrapper{pre}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if (ref $return eq 'ARRAY' && $return == $prereturn && !@$return) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
ref $return eq "ARRAY" and $return == $prereturn and not @$return
ref $return eq "ARRAY" and $return == $prereturn
MC/DC0%0/3
ref $return eq "ARRAY" and $return == $prereturn and not @$return
ref $return eq "ARRAY"
$return == $prereturn
not @$return
Truth table0%0/4
A and B and not(C)
A= ref $return eq "ARRAY"B= $return == $prereturnC= not @$return
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $return = [ &$original ]; | |||||||||||||||||||||||||||||||||||||||||
| 53 | () = $wrapper{post}->(@_, $return) | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if $wrapper{post}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return ref $return eq 'ARRAY' ? @$return : ($return); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | elsif (defined wantarray) { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $return = bless sub {$prereturn=1}, 'PerlGuard::Agent::LexWrap::Cleanup'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $dummy = $wrapper{pre}->(\@_, $return) if $wrapper{pre}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless ($prereturn) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $return = &$original; | |||||||||||||||||||||||||||||||||||||||||
| 63 | $dummy = scalar $wrapper{post}->(@_, $return) | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if $wrapper{post}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return $return; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $return = bless sub {$prereturn=1}, 'PerlGuard::Agent::LexWrap::Cleanup'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $wrapper{pre}->(\@_, $return) if $wrapper{pre}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | unless ($prereturn) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | &$original; | |||||||||||||||||||||||||||||||||||||||||
| 73 | $wrapper{post}->(@_, $return) | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if $wrapper{post}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | ref $typeglob eq 'CODE' and return defined wantarray | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | ? $imposter | ||||||||||||||||||||||||||||||||||||||||||
| 81 | : Carp::carp "Uselessly wrapped subroutine reference in void context"; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | *{$typeglob} = $imposter; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return unless defined wantarray; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | return bless sub{ $unwrap=1 }, 'PerlGuard::Agent::LexWrap::Cleanup'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | package PerlGuard::Agent::LexWrap::Cleanup; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub DESTROY { $_[0]->() } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | q{""} => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | q{0+} => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | q{bool} => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | q{fallback}=>1; #fallback=1 - like no overloading for other operations | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||||||||||||||||||||||||