blib/lib/ogd.pm

filter: Statementstmt 57.1% 36 / 63 Branchbran 15.3% 4 / 26 Conditioncond 20.0% 2 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 71.4% 10 / 14 Podpod 50.0% 1 / 2 totaltotal 43.8% 53 / 121 SCAR 41.7
CC
16
Cov
42%
register
42.0
__ANON__
12.0
_shutting_down
11.5
CRAP 64.9
↑ Summary
1 package ogd;
2 1 require 5.008001; # must have a good B in the core
3  
4 # Make sure we have version info for this module
5 # Make sure we do everything by the book from now on
6  
7 1 $VERSION = '0.04';
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # At compile time
11 # Create boolean for debug state
12 # Create constant with that boolean
13 # Create value for cleanup check
14 # Create constant with that value
15  
16 BEGIN {
17 1 ▶ my $debug = ($ENV{OGD_DEBUG} || '') =~ m#^(\d+)$# ? $1 : '';
Subroutine BEGIN: called CC=5 SCAR=17.8
Branch: 1 branch
BranchTrueFalseTotal
($ENV{'OGD_DEBUG'} || '') =~ /^(\d+)$/ ? : 0 1 1
Condition50%1/2
$ENV{'OGD_DEBUG'} || ''
l!l
01
MC/DC0%0/1
$ENV{'OGD_DEBUG'} || ''
$ENV{'OGD_DEBUG'}
Truth table50%1/2
A
A= $ENV{'OGD_DEBUG'}
Aresult
11
00
18 1 ▶ eval "sub DEBUG () { $debug }";
Subroutine DEBUG: called CC=1 SCAR=0.0
Pod undocumented
19 1 ▶ my $cleanup = ($ENV{OGD_CLEANUP} || '') =~ m#^(\d+)$# ? $1 : 10;
Branch: 1 branch
BranchTrueFalseTotal
($ENV{'OGD_CLEANUP'} || '') =~ /^(\d+)$/ ? : 0 1 1
Condition50%1/2
$ENV{'OGD_CLEANUP'} || ''
l!l
01
MC/DC0%0/1
$ENV{'OGD_CLEANUP'} || ''
$ENV{'OGD_CLEANUP'}
Truth table50%1/2
A
A= $ENV{'OGD_CLEANUP'}
Aresult
11
00
20 1 eval "sub CLEANUP () { $cleanup }";
21 } #BEGIN
22  
23 # Make sure we can find out the blessing of an object and to weaken it
24  
25 1 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # Initialize counter for number of objects registered
28 # List with objects that should be destroyed (first is a dummy object)
29  
30 1 my $registered = 0;
31 1 my @object;
32  
33 # Make sure we do this before anything else
34 # Allow for dirty tricks
35 # Obtain current setting
36 # See if we can call it
37 # Use the core one if it was an empty subroutine reference
38  
39 BEGIN {
40 1 ▶ no strict 'refs'; no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶ my $old = \&CORE::GLOBAL::bless;
Subroutine BEGIN: called CC=2 SCAR=10.9
42 1 eval {$old->()};
43 1 ▶ $old = undef if $@ =~ m#CORE::GLOBAL::bless#;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /CORE::GLOBAL::bless/ 1 0 1
44  
45 # Steal the system bless with a sub
46 # Obtain the class
47 # Create the object with the given parameters
48 # Register object
49 # Return the blessed object
50  
51 *CORE::GLOBAL::bless = sub {
52 0 ▶ my $class = $_[1] || caller();
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/3
$_[1] || caller()
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] || caller()
$_[1] caller()
Truth table0%0/3
A || B
A= $_[1]B= caller()
ABresult
1X1
011
000
53 0 ▶ my $object = $old ? $old->( $_[0],$class ) : CORE::bless $_[0],$class;
Branch: 1 branch
BranchTrueFalseTotal
$old ? : 0 0 0
54 0 __PACKAGE__->register( $object );
55 0 $object;
56 1 };
57 } #BEGIN
58  
59 # When Perl is shutting down
60 # Make sure we can do the nifty internal stuff
61 # Push the shutting down sequence as the very last thing we'll do
62  
63 END {
64 1 ▶ require B;
Subroutine END: called CC=1 SCAR=0.0
65 1 push @{B::end_av()->object_2svref},\&_shutting_down;
66 1 ▶print STDERR "*\n" if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 1 1
67 } #END
68  
69 # Satisfy -require-
70  
71 1 1;
72  
73 #---------------------------------------------------------------------------
74 #
75 # Class methods
76 #
77 #---------------------------------------------------------------------------
78 # register
79 #
80 # Register one or more objects with ogd. Also used internally.
81 #
82 # IN: 1 class (ignored)
83 # 2..N objects to register
84  
85 sub register {
86  
87 # Lose the class
88 # Store weakened references to the object in the global list
89  
90 0 ▶ shift;
Subroutine register: not called CC=6 SCAR=37.4
Pod documented
91 0 ▶print STDERR "+".(@_)."\n" if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 0 0
92 0 weaken( $object[@object] = $_ ) foreach @_;
93  
94 # Remember current number of objects registered (ever)
95 # Increment for number of objects registered
96 # If a cleanup is needed
97 # Remember the number of objects before cleanup started
98 # For all of the elements in reverse order (must access by index!)
99 # Remove the entry if it is not defined
100  
101 0 my $old = $registered;
102 0 $registered += @_;
103 0 ▶ if (($registered >> CLEANUP) > ($old >> CLEANUP)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($registered >> 10 > $old >> 10) 0 0 0
104 0 my $before = @object;
105 0 foreach (reverse 0..$#object) {
106 0 ▶ splice @object,$_,1 unless defined $object[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object[$_] 0 0 0
107 }
108 0 ▶print STDERR "-$before->".(@object)."\n" if DEBUG and $before > @object;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() and $before > @object 0 0 0
Condition0%0/3
&DEBUG() and $before > @object
!ll&&!rl&&r
000
MC/DC0%0/2
&DEBUG() and $before > @object
&DEBUG() $before > @object
Truth table0%0/3
A and B
A= &DEBUG()B= $before > @object
ABresult
0X0
100
111
109 }
110 } #register
111  
112 #---------------------------------------------------------------------------
113 #
114 # Internal methods
115 #
116 #---------------------------------------------------------------------------
117 # _shutting_down
118 #
119 # The subroutine that will be called at the very, very end
120  
121 sub _shutting_down {
122  
123 # Initialize hash with packages handled
124 # Initialize counter of how many done
125 # While there are objects to process
126 # Obtain newest object, reloop if it is already dead
127 # Mark the package as used
128 # Execute the DESTROY method on it (let it know it's being forced)
129  
130 0 ▶ my %package;
Subroutine _shutting_down: not called CC=4 SCAR=24.4
131 0 my $done = 0;
132 0 foreach (reverse 0..$#object) {
133 0 ▶ next unless defined $object[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object[$_] 0 0 0
134 0 $package{blessed $object[$_]}++;
135 0 $object[$_]->DESTROY( 1 );
136 0 ▶$done++ if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 0 0
137 }
138 0 ▶print STDERR "!$done\n" if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 0 0
139  
140 # Make sure we'll be silent about the dirty stuff
141 # Replace DESTROY subs of all packages found with an empty stub
142  
143 1 ▶ no strict 'refs'; no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
144 0 ▶print STDERR qq{x@{[map { "$_($package{$_})" } sort keys %package]}\n} if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if &DEBUG() 0 0 0
145 0 *{$_.'::DESTROY'} = \&_destroy foreach keys %package;
146 } #_shutting_down
147  
148 #---------------------------------------------------------------------------
149 # _destroy
150 #
151 # IN: 1 instantiated object (ignored)
152 #
153 # This is the empty DESTROY stub that replaces any actual DESTROY subs
154 # after all objects have been destroyed.
155  
156 ▶sub _destroy { } #_destroy
Subroutine _destroy: not called CC=1 SCAR=0.0
157  
158 #---------------------------------------------------------------------------
159  
160 __END__
↑ Summary