| 1 | package Function::Override; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use vars qw( $Debug $VERSION @EXPORT ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 2 | ▶ | use base qw(Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | $VERSION = '0.03'; | |||||||||||||||||||
| 10 | 1 | @EXPORT = qw(override); | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | ▶ | $Debug = $ENV{PERL_FUNCTION_OVERRIDE_DEBUG} || 0 unless defined $Debug; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$ENV{'PERL_FUNCTION_OVERRIDE_DEBUG'} || 0
MC/DC0%0/1
$ENV{'PERL_FUNCTION_OVERRIDE_DEBUG'} || 0
$ENV{'PERL_FUNCTION_OVERRIDE_DEBUG'}
Truth table50%1/2
A
A= $ENV{'PERL_FUNCTION_OVERRIDE_DEBUG'}
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 3 | ▶ | sub override { | ||||||||||||||||||
|
Subroutine
override: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 15 | 3 | my($sym, $callback, $pkg) = @_; | |||||||||||||||||||
| 16 | 3 | ▶ | $pkg = caller() unless defined $pkg; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | 3 | &_override_function($sym, $callback, $pkg); | |||||||||||||||||||
| 18 | }; | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 3 | ▶ | sub fill_protos { | ||||||||||||||||||
|
Subroutine
fill_protos: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||
| 21 | 3 | my $proto = shift; | |||||||||||||||||||
| 22 | 3 | my ($n, $isref, @out, @out1, $seen_semi) = -1; | |||||||||||||||||||
| 23 | 3 | ▶ | while ($proto =~ /\S/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | 6 | $n++; | |||||||||||||||||||
| 25 | 6 | ▶ | push(@out1,[$n,@out]) if $seen_semi; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | 6 | ▶ | push(@out, $1 . "{\$_[$n]}"), next if $proto =~ s/^\s*\\([\@%\$\&])//; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 6 | ▶ | push(@out, "\$_[$n]"), next if $proto =~ s/^\s*([*\$&_])//; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 3 | ▶ | push(@out, "\@_[$n..\$#_]"), last if $proto =~ s/^\s*(;\s*)?\@//; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 1 | ▶ | $seen_semi = 1, $n--, next if $proto =~ s/^\s*;//; # XXXX ???? | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 0 | die "Unknown prototype letters: \"$proto\""; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | 3 | push(@out1,[$n+1,@out]); | |||||||||||||||||||
| 33 | 3 | @out1; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 3 | ▶ | sub write_invocation { | ||||||||||||||||||
|
Subroutine
write_invocation: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 37 | 3 | my ($core, $call, $name, @argvs) = @_; | |||||||||||||||||||
| 38 | 3 | ▶ | if (@argvs == 1) { # No optional arguments | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 2 | my @argv = @{$argvs[0]}; | |||||||||||||||||||
| 40 | 2 | shift @argv; | |||||||||||||||||||
| 41 | 2 | return "\t" . one_invocation($core, $call, $name, @argv) . ";\n"; | |||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||
| 43 | 1 | my $else = "\t"; | |||||||||||||||||||
| 44 | 1 | my (@out, @argv, $n); | |||||||||||||||||||
| 45 | 1 | ▶ | while (@argvs) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 3 | @argv = @{shift @argvs}; | |||||||||||||||||||
| 47 | 3 | $n = shift @argv; | |||||||||||||||||||
| 48 | 3 | push @out, "$ {else}if (\@_ == $n) {\n"; | |||||||||||||||||||
| 49 | 3 | $else = "\t} els"; | |||||||||||||||||||
| 50 | 3 | push @out, | |||||||||||||||||||
| 51 | "\t\treturn " . one_invocation($core, $call, $name, @argv) . ";\n"; | ||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | 1 | push @out, <<EOC; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | die "$name(\@_): Do not expect to get ", scalar \@_, " arguments"; | ||||||||||||||||||||
| 56 | EOC | ||||||||||||||||||||
| 57 | 1 | return join '', @out; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 5 | ▶ | sub one_invocation { | ||||||||||||||||||
|
Subroutine
one_invocation: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 62 | 5 | my ($core, $call, $name, @argv) = @_; | |||||||||||||||||||
| 63 | 5 | local $" = ', '; | |||||||||||||||||||
| 64 | 5 | return qq{$call(@argv)}; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 3 | ▶ | sub _override_function { | ||||||||||||||||||
|
Subroutine
_override_function: called CC=12 SCAR=27.1
| |||||||||||||||||||||
| 68 | 3 | my($sub, $callback, $pkg) = @_; | |||||||||||||||||||
| 69 | 3 | my($name, $code, $sref, $real_proto, $proto, $core, $call); | |||||||||||||||||||
| 70 | 3 | my $ini = $sub; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 3 | ▶ | $sub = "${pkg}::$sub" unless $sub =~ /::/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 3 | $name = $sub; | |||||||||||||||||||
| 74 | 3 | ▶ | $name =~ s/.*::// or $name =~ s/^&//; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 3 | ▶ | print "# _override_function: sub=$sub pkg=$pkg name=$name\n" if $Debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 3 | ▶ | croak "Bad subroutine name for Function::Override: $name" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | unless $name =~ /^\w+$/; | ||||||||||||||||||||
| 78 | 3 | ▶ | if (defined(&$sub)) { # user subroutine | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 79 | 2 | $sref = \&$sub; | |||||||||||||||||||
| 80 | 2 | $proto = prototype $sref; | |||||||||||||||||||
| 81 | 2 | $call = '&$sref'; | |||||||||||||||||||
| 82 | } elsif ($sub eq $ini) { # Stray user subroutine | ||||||||||||||||||||
| 83 | 0 | die "$sub is not a Perl subroutine" | |||||||||||||||||||
| 84 | } else { # CORE subroutine | ||||||||||||||||||||
| 85 | 1 | $proto = eval { prototype "CORE::$name" }; | |||||||||||||||||||
| 86 | 1 | ▶ | die "$name is neither a builtin, nor a Perl subroutine" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | if $@; | ||||||||||||||||||||
| 88 | 1 | ▶ | die "Cannot override the non-overridable builtin '$name'" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | if not defined $proto; | ||||||||||||||||||||
| 90 | 1 | $core = 1; | |||||||||||||||||||
| 91 | 1 | $call = "CORE::$name"; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 3 | ▶ | if (defined $proto) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 2 | $real_proto = " ($proto)"; | |||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||
| 96 | 1 | $real_proto = ''; | |||||||||||||||||||
| 97 | 1 | $proto = '@'; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | 3 | $code = <<EOS; | |||||||||||||||||||
| 100 | sub$real_proto { | ||||||||||||||||||||
| 101 | local(\$", \$!) = (', ', 0); | ||||||||||||||||||||
| 102 | \$callback->(\@_); | ||||||||||||||||||||
| 103 | EOS | ||||||||||||||||||||
| 104 | 3 | my @protos = fill_protos($proto); | |||||||||||||||||||
| 105 | 3 | $code .= write_invocation($core, $call, $name, @protos); | |||||||||||||||||||
| 106 | 3 | $code .= "}\n"; | |||||||||||||||||||
| 107 | { | ||||||||||||||||||||
| 108 | 2 | ▶ | no strict 'refs'; # to avoid: Can't use string (...) as a symbol ref ... | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 109 | 3 | $code = <<"CODE"; | |||||||||||||||||||
| 110 | package $pkg; | ||||||||||||||||||||
| 111 | $code | ||||||||||||||||||||
| 112 | CODE | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 3 | ▶ | print $code if $Debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | ▶ | $code = eval($code); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=3.1
Branch: 3 branches
| |||||||||||||||||||||
| 117 | 3 | ▶ | die if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 3 | local($^W) = 0; # to avoid: Subroutine foo redefined ... | |||||||||||||||||||
| 119 | 3 | *{$sub} = $code; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 1 | 1; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||