blib/lib/Sub/Versive.pm

filter: Statementstmt 85.7% 72 / 84 Branchbran 40.0% 8 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 3 totaltotal 76.6% 92 / 120 CC 14 SCAR 13.6
CC
14
Cov
77%
bar
6.0
foo
6.0
__ANON__
6.0
CRAP 3.9
↑ Summary
1 package Sub::Versive;
2  
3 1 require 5.6.1;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Devel::Peek q/CvGV/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶no warnings 'redefine'; # Oh yes.
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11  
12 1 our @ISA = qw(Exporter);
13  
14 # Items to export into callers namespace by default. Note: do not export
15 # names by default without a very good reason. Use EXPORT_OK instead.
16 # Do not simply export all your public functions/methods/constants.
17  
18 # This allows declaration use Sub::Versive ':all';
19 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
20 # will save memory.
21 1 our %EXPORT_TAGS = ( 'all' => [ qw(
22 append_to_sub
23 prepend_to_sub
24 builtinify
25 ) ] );
26  
27 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
28  
29 1 our @EXPORT = qw(
30
31 );
32 1 our $VERSION = '0.01';
33  
34  
35 # This *is* pure evil.
36  
37 4 ▶sub _prep {
Subroutine _prep: called CC=4 SCAR=14.7
38 4 my $orig = shift;
39 4 my $ref;
40 4 ▶ if (ref $orig eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $orig eq 'CODE') { } 4 0 4
41 4 $ref = $orig;
42 4 $orig = CvGV($orig);
43 4 $orig =~ s/.//;
44 } else {
45 0 $ref = eval "\\\&$orig";# This is evil too.
46 }
47 4 ▶ if (not exists $Sub::Version::stash{$orig}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Sub::Version::stash{$orig}) 2 2 4
48 2 $Sub::Version::stash{$orig}{orig} = $ref;
49 2 my $code = 'sub '.$orig.' {
50 for (@{$Sub::Version::stash{"'.$orig.'"}{precall}}) {
51 my @x = $_->(@_);
52 return @x if ($@); # Signal a return
53 }
54 my @rv = $Sub::Version::stash{"'.$orig.'"}{orig}->(@_);
55 for (@{$Sub::Version::stash{"'.$orig.'"}{postcall}}) {
56 my @x = $_->(@_);
57 return @x if ($@); # Signal a return
58 }
59 return @rv;
60 }';
61 0 ▶ eval $code; $@ and die "$code:$@";
Subroutine bar: called CC=5 SCAR=17.9
Subroutine foo: called CC=5 SCAR=17.9
Branch: 5 branches
BranchTrueFalseTotal
$@ and die "${code}:$@" 0 2 2
if $@ 0 0 0
if $@ 0 3 3
if $@ 0 1 1
if $@ 0 0 0
62 }
63 4 return ($ref, $orig);
64 }
65 3 ▶sub append_to_sub (&\&) {
Subroutine append_to_sub: called CC=1 SCAR=0.0
Pod undocumented
66 3 my ($newcode, $orig) = @_;
67 3 my $ref;
68 3 ($ref, $orig) = _prep($orig);
69 3 push @{$Sub::Version::stash{$orig}{postcall}}, $newcode;
70 };
71  
72 1 ▶sub prepend_to_sub (&\&) {
Subroutine prepend_to_sub: called CC=1 SCAR=0.0
Pod undocumented
73 1 my ($newcode, $orig) = @_;
74 1 my $ref;
75 1 ($ref, $orig) = _prep($orig);
76 1 unshift @{$Sub::Version::stash{$orig}{precall}}, $newcode;
77 };
78  
79 # Let's go, guys!
80  
81 1 ▶if (defined &UNIVERSAL::AUTOLOAD) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &UNIVERSAL::AUTOLOAD) { } 0 1 1
82 0 ▶prepend_to_sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
83 0 my $foo = $UNIVERSAL::AUTOLOAD;
84 0 $foo =~ s/.*:://;
85 0 ▶ if (exists $Sub::Versive::builtins{$foo}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Sub::Versive::builtins{$foo}) 0 0 0
86 0 $@="Die alien swine!";
87 0 return $Sub::Versive::builtins{$foo}->(@_);
88 }
89 0 } &UNIVERSAL::AUTOLOAD;
90 }
91 else {
92 0 ▶ eval <<'EOF';
Subroutine AUTOLOAD: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Sub::Versive::builtins{$foo}) 0 72693 72693
93 sub UNIVERSAL::AUTOLOAD {
94 my $foo = $UNIVERSAL::AUTOLOAD;
95 $foo =~ s/.*:://;
96 if (exists $Sub::Versive::builtins{$foo}) {
97 return $Sub::Versive::builtins{$foo}->(@_);
98 }
99 # Fake it.
100 croak "Undefined subroutine $UNIVERSAL::AUTOLOAD called";
101 }
102 EOF
103 }
104  
105 1 ▶sub builtinify (\&) {
Subroutine builtinify: called CC=1 SCAR=0.0
Pod undocumented
106 1 my $sub = shift;
107 1 my $whence = CvGV($sub);
108 1 $whence =~ s/.*:://;
109 1 $Sub::Versive::builtins{$whence} = $sub;
110 }
111 1 1;
112 __END__
↑ Summary