blib/lib/Object/ForkAware.pm

filter: Statementstmt 100% 43 / 43 Branchbran 95.8% 23 / 24 Conditioncond 57.1% 8 / 14 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 90.9% 10 / 11 Podpod 25.0% 1 / 4 totaltotal 85.2% 87 / 102 CC 18 SCAR 15.0
CC
18
Cov
91%
_get_obj
9.1
new
4.0
_create_obj
3.0
CRAP 4.5
↑ Summary
1 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Object::ForkAware; # git description: v0.004-4-g2bf9955
4 # vim: set ts=8 sts=4 sw=4 tw=115 et :
5 # ABSTRACT: Make an object aware of process forks and threads, recreating itself as needed
6 # KEYWORDS: process thread fork multiprocessing multithreading clone
7  
8 10 our $VERSION = '0.005';
9  
10 10 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new
13 13 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod documented
14 13 my ($class, %opts) = @_;
15  
16 13 my $self = {};
17 13 ▶ $self->{_create} = $opts{create} or die 'missing required option: create';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_create"} = $opts{"create"} or die "missing required option: create" 12 1 13
18 12 ▶ $self->{_on_fork} = $opts{on_fork} if exists $opts{on_fork};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"on_fork"} 5 7 12
19  
20 12 $self = bless($self, $class);
21  
22 12 ▶ $self->_create_obj($self->{_create}) if not $opts{lazy};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"lazy"} 7 5 12
23  
24 12 return $self;
25 }
26  
27 sub _create_obj
28 15 ▶{
Subroutine _create_obj: called CC=3 SCAR=11.0
29 15 my ($self, $sub) = @_;
30  
31 15 ▶ my $obj = $sub->( defined $self->{_obj} ? $self->{_obj} : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_obj'} ? : 4 11 15
32 14 $self->{_pid} = $$;
33 14 ▶ $self->{_tid} = threads->tid if $INC{'threads.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 14 14
34 14 $self->{_obj} = $obj;
35 }
36  
37 sub _get_obj
38 54 ▶{
Subroutine _get_obj: called CC=8 SCAR=22.1
39 54 my $self = shift;
40  
41 54 ▶ return if not Scalar::Util::blessed($self);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 5 49 54
42 49 ▶ if (not defined $self->{_pid}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_pid"} or $$ != $self->{"_pid"} or $INC{"threads.pm"} and ($self->{"_tid"} || 0) != "threads"->tid) 8 41 49
Condition54%6/11
not defined $self->{"_pid"} or $$ != $self->{"_pid"} or $INC{"threads.pm"} and ($self->{"_tid"} || 0) != "threads"->tid
l!l&&r!l&&!r
8041
not defined $self->{"_pid"} or $$ != $self->{"_pid"}
l!l&&r!l&&!r
4441
$INC{"threads.pm"} and ($self->{"_tid"} || 0) != "threads"->tid
!ll&&!rl&&r
4100
$self->{'_tid'} || 0
l!l
00
MC/DC50%2/4
not defined $self->{"_pid"} or $$ != $self->{"_pid"} or $INC{"threads.pm"} and ($self->{"_tid"} || 0) != "threads"->tid
not defined $self->{"_pid"} $$ != $self->{"_pid"} $INC{"threads.pm"} $self->{'_tid'}
Truth table60%3/5
not(A) or B or C and D
A= not defined $self->{"_pid"}B= $$ != $self->{"_pid"}C= $INC{"threads.pm"}D= $self->{'_tid'}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
43 or $$ != $self->{_pid}
44 or $INC{'threads.pm'} and ($self->{_tid} || 0) != threads->tid)
45 {
46 8 ▶ $self->_create_obj($self->{_on_fork} || $self->{_create});
Condition66%2/3
$self->{'_on_fork'} || $self->{'_create'}
l!l&&r!l&&!r
260
MC/DC0%0/2
$self->{'_on_fork'} || $self->{'_create'}
$self->{'_on_fork'} $self->{'_create'}
Truth table66%2/3
A || B
A= $self->{'_on_fork'}B= $self->{'_create'}
ABresult
1X1
011
000
47 }
48  
49 48 return $self->{_obj};
50 }
51  
52 sub isa
53 13 ▶{
Subroutine isa: called CC=3 SCAR=11.0
Pod undocumented
54 13 my ($self, $class) = @_;
55 13 ▶ $self->SUPER::isa($class) || do {
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::isa($class) or do { my $obj = $self->_get_obj; $obj and $obj->isa($class) } 4 9 13
56 9 my $obj = $self->_get_obj;
57 9 ▶ $obj && $obj->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->isa($class) 7 2 9
58 };
59 }
60  
61 sub can
62 8 ▶{
Subroutine can: called CC=3 SCAR=11.0
Pod undocumented
63 8 my ($self, $method) = @_;
64 8 ▶ $self->SUPER::can($method) || do {
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::can($method) or do { my $obj = $self->_get_obj; $obj and $obj->can($method) } 1 7 8
65 7 my $obj = $self->_get_obj;
66 7 ▶ $obj && $obj->can($method);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->can($method) 6 1 7
67 };
68 }
69  
70 sub VERSION
71 8 ▶{
Subroutine VERSION: called CC=2 SCAR=6.9
Pod undocumented
72 8 my ($self, @args) = @_;
73  
74 8 my $obj = $self->_get_obj;
75 8 ▶ return $obj
Branch: 1 branch
BranchTrueFalseTotal
$obj ? : 6 2 8
76 ? $obj->VERSION(@args)
77 : $self->SUPER::VERSION(@args);
78 }
79  
80 10 our $AUTOLOAD;
81 sub AUTOLOAD
82 30 ▶{
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
83 30 my $self = shift;
84  
85 # Remove qualifier from original method name...
86 30 (my $called = $AUTOLOAD) =~ s/.*:://;
87 30 return $self->_get_obj->$called(@_);
88 }
89  
90 0 ▶sub DESTROY {} # avoid calling AUTOLOAD at destruction time
Subroutine DESTROY: not called CC=1 SCAR=0.0
91  
92 10 1;
93  
94 __END__
↑ Summary