blib/lib/Sub/Clone.pm untested

filter: Statementstmt 0.0% 0 / 30 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 46 CC 2 SCAR 11.5
CC
2
Cov
0%
BEGIN
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.1
1 #!/usr/bin/perl
2  
3 package Sub::Clone;
4  
5 use strict;
6 use warnings;
7  
8 BEGIN {
9 our $VERSION = '0.03';
10  
11 my $e = do {
12 local $@;
13  
14 eval {
15 require XSLoader;
16 __PACKAGE__->XSLoader::load($VERSION);
17 };
18
19 $@;
20 };
21  
22 if ( $e && $e !~ /object version|loadable object/ ) {
23 warn $e;
24 require DynaLoader;
25 push our @ISA, 'DynaLoader';
26 __PACKAGE__->bootstrap($VERSION);
27 }
28 }
29  
30 use Sub::Exporter -setup => {
31 exports => [qw(is_cloned clone_sub clone_if_immortal)],
32 groups => { default => [qw(is_cloned clone_sub)] },
33 };
34  
35 # Pure Perl implementation:
36 unless ( defined &is_cloned ) {
37 eval '
38 use B qw(svref_2object CVf_CLONED);
39 use Scalar::Util qw(blessed);
40  
41 sub is_cloned ($) {
42 my $sub = shift;
43 svref_2object($sub)->CvFLAGS & CVf_CLONED;
44 }
45  
46 sub clone_sub ($) {
47 my $sub = shift;
48 my $clone = sub { goto $sub };
49  
50 if ( defined( my $class = blessed($sub) ) ) {
51 bless $clone, $class;
52 }
53  
54 return $clone;
55 }
56  
57 sub clone_if_immortal ($) {
58 my $sub = shift;
59 is_cloned($sub) ? $sub : clone_sub($sub)
60 }
61 ';
62 }
63  
64 __PACKAGE__
65  
66 __END__