| 1 | # | ||||||||||||||||||||||
| 2 | # $Id: Decycle.pm,v 0.2 2010/08/23 09:11:03 dankogai Exp dankogai $ | ||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||
| 4 | package Data::Decycle; | ||||||||||||||||||||||
| 5 | 6 | ▶ | use 5.008001; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 6 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 6 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 6 | ▶ | use Scalar::Util qw/refaddr weaken isweak/; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 6 | our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; | |||||||||||||||||||||
| 12 | 6 | our $DEBUG = 0; | |||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 6 | ▶ | use base 'Exporter'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | 6 | our @EXPORT = (); | |||||||||||||||||||||
| 16 | 6 | our @EXPORT_OK = qw(recsub $CALLEE | |||||||||||||||||||||
| 17 | may_leak has_cyclic_ref decycle_deeply weaken_deeply | ||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||
| 19 | 6 | our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ], ); | |||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | 6 | ▶ | BEGIN { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 22 | 6 | require constant; | |||||||||||||||||||||
| 23 | constant->import( | ||||||||||||||||||||||
| 24 | 6 | HAS_PADWALKER => eval { | |||||||||||||||||||||
| 25 | 6 | require PadWalker; | |||||||||||||||||||||
| 26 | 6 | $PadWalker::VERSION >= 1.0; | |||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | 0 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 32 | 0 | my $class = shift; | |||||||||||||||||||||
| 33 | 0 | my $self = bless [], $class; | |||||||||||||||||||||
| 34 | 0 | $self->add(@_); | |||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 0 | ▶ | sub add { | ||||||||||||||||||||
|
Subroutine
add: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||
| 38 | 0 | my $self = shift; | |||||||||||||||||||||
| 39 | 0 | for (@_){ | |||||||||||||||||||||
| 40 | 0 | ▶ | croak "$_ is not a reference" unless ref $_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 41 | 0 | push @{$self}, $_; | |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||
| 43 | 0 | $self; | |||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||
| 47 | 0 | my $self = shift; | |||||||||||||||||||||
| 48 | 0 | ▶ | if ($DEBUG > 1){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 49 | 0 | ▶ | require Data::Dumper and Data::Dumper->import; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 50 | 0 | print Dumper($self); | |||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||
| 52 | 0 | for (@{$self}){ | |||||||||||||||||||||
| 53 | 0 | ▶ | next unless ref $_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 54 | 0 | ▶ | carp "decyling ($_)" if $DEBUG; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 55 | 0 | decycle_deeply($_); | |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | 6 | our $CALLEE; | |||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | 27 | ▶ | sub recsub(&) { | ||||||||||||||||||||
|
Subroutine
recsub: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 62 | 27 | my $code = shift; | |||||||||||||||||||||
| 63 | 47 | ▶ | sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 64 | 47 | local *CALLEE = \$code; | |||||||||||||||||||||
| 65 | 47 | $code->(@_); | |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||
| 67 | 27 | } | |||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | 12 | ▶ | sub _mkfinder(&) { | ||||||||||||||||||||
|
Subroutine
_mkfinder: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 70 | 12 | my $cb = shift; | |||||||||||||||||||||
| 71 | 70 | ▶ | return recsub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=16 SCAR=27.7
| |||||||||||||||||||||||
| 72 | 70 | ▶ | return unless ref $_[0]; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 73 | 6 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 74 | 64 | ▶ | return $cb->( $_[0] ) if $_[1]->{ refaddr $_[0] }++; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 75 | 47 | ▶ | if ( UNIVERSAL::isa( $_[0], 'HASH' ) ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 76 | 15 | for ( values %{ $_[0] } ) { | |||||||||||||||||||||
| 77 | 18 | ▶ | next unless ref $_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 78 | 12 | ▶ | return 1 if $CALLEE->( $_, $_[1] ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||
| 81 | elsif ( UNIVERSAL::isa( $_[0], 'ARRAY' ) ) { | ||||||||||||||||||||||
| 82 | 8 | for ( @{ $_[0] } ) { | |||||||||||||||||||||
| 83 | 13 | ▶ | next unless ref $_; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 84 | 4 | ▶ | return 1 if $CALLEE->( $_, $_[1] ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||
| 87 | ▶ | elsif (UNIVERSAL::isa( $_[0], 'SCALAR' ) | |||||||||||||||||||||
|
Condition66%2/3
UNIVERSAL::isa($_[0], "SCALAR") or UNIVERSAL::isa($_[0], "REF")
MC/DC50%1/2
UNIVERSAL::isa($_[0], "SCALAR") or UNIVERSAL::isa($_[0], "REF")
UNIVERSAL::isa($_[0], "SCALAR")
UNIVERSAL::isa($_[0], "REF")
Truth table66%2/3
A or B
A= UNIVERSAL::isa($_[0], "SCALAR")B= UNIVERSAL::isa($_[0], "REF")
| |||||||||||||||||||||||
| 88 | || UNIVERSAL::isa( $_[0], 'REF' ) ) | ||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||
| 90 | 11 | return $CALLEE->( ${ $_[0] }, $_[1] ); | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | elsif ( HAS_PADWALKER && UNIVERSAL::isa( $_[0], 'CODE' ) ) { | ||||||||||||||||||||||
| 93 | 13 | my $r = PadWalker::closed_over( $_[0] ); | |||||||||||||||||||||
| 94 | 13 | ▶ | return unless keys %$r; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 95 | 7 | ▶ | $CALLEE->( $r, $_[1] ) && return 1; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||
| 97 | 15 | return; | |||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||
| 99 | 12 | } | |||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 10 | ▶ | *_has_cyclic_ref = _mkfinder { 1 }; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 102 | 24 | ▶ | sub has_cyclic_ref($){ _has_cyclic_ref($_[0], {}) } | ||||||||||||||||||||
|
Subroutine
has_cyclic_ref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 103 | |||||||||||||||||||||||
| 104 | 7 | ▶ | *_may_leak = _mkfinder { !isweak($_[0]) }; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 105 | 12 | ▶ | sub may_leak($){ _may_leak($_[0], {}) } | ||||||||||||||||||||
|
Subroutine
may_leak: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 12 | ▶ | sub _mkwalker(&){ | ||||||||||||||||||||
|
Subroutine
_mkwalker: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 108 | 12 | my $cb = shift; | |||||||||||||||||||||
| 109 | 30 | ▶ | return recsub { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.1
| |||||||||||||||||||||||
| 110 | 30 | ▶ | return unless ref $_[0]; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 111 | 6 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 112 | 26 | ▶ | return $cb->( $_[0] ) if $_[1]->{ refaddr $_[0] }++; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 113 | 15 | ▶ | if ( UNIVERSAL::isa( $_[0], 'HASH' ) ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 114 | 5 | $CALLEE->( $_, $_[1] ) for values %{ $_[0] }; | |||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||
| 116 | elsif ( UNIVERSAL::isa( $_[0], 'ARRAY' ) ) { | ||||||||||||||||||||||
| 117 | 3 | $CALLEE->( $_, $_[1] ) for @{ $_[0] }; | |||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||
| 119 | ▶ | elsif (UNIVERSAL::isa( $_[0], 'SCALAR' ) | |||||||||||||||||||||
|
Condition66%2/3
UNIVERSAL::isa($_[0], "SCALAR") or UNIVERSAL::isa($_[0], "REF")
MC/DC50%1/2
UNIVERSAL::isa($_[0], "SCALAR") or UNIVERSAL::isa($_[0], "REF")
UNIVERSAL::isa($_[0], "SCALAR")
UNIVERSAL::isa($_[0], "REF")
Truth table66%2/3
A or B
A= UNIVERSAL::isa($_[0], "SCALAR")B= UNIVERSAL::isa($_[0], "REF")
| |||||||||||||||||||||||
| 120 | || UNIVERSAL::isa( $_[0], 'REF' ) ) | ||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||
| 122 | 5 | $CALLEE->( ${ $_[0] }, $_[1] ); | |||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||
| 124 | elsif ( HAS_PADWALKER && UNIVERSAL::isa( $_[0], 'CODE' ) ) { | ||||||||||||||||||||||
| 125 | 2 | my $r = PadWalker::closed_over( $_[0] ); | |||||||||||||||||||||
| 126 | 2 | ▶ | return unless keys %$r; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 127 | 2 | $CALLEE->( $r, $_[1] ); | |||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||
| 129 | 15 | return; | |||||||||||||||||||||
| 130 | 12 | }; | |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | 4 | ▶ | *_decycle_deeply = _mkwalker { undef $_[0] }; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 134 | 4 | ▶ | sub decycle_deeply($) { _decycle_deeply( $_[0], {} ) } | ||||||||||||||||||||
|
Subroutine
decycle_deeply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | 7 | ▶ | *_weaken_deeply = _mkwalker { | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 137 | 7 | ▶ | weaken $_[0] unless UNIVERSAL::isa( $_[0], 'CODE' ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 138 | 6 | }; | |||||||||||||||||||||
| 139 | 7 | ▶ | sub weaken_deeply($) { _weaken_deeply( $_[0], {} ) } | ||||||||||||||||||||
|
Subroutine
weaken_deeply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | 6 | 1; # End of Data::Decycle | |||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||