| 1 | package Object::InsideOut; { | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # Installs object dumper and loader methods | ||||||||||||||||||||||||||||
| 8 | sub dump | ||||||||||||||||||||||||||||
| 9 | { | ||||||||||||||||||||||||||||
| 10 | my ($GBL, $call, @args) = @_; | ||||||||||||||||||||||||||||
| 11 | push(@{$$GBL{'export'}}, 'dump'); | ||||||||||||||||||||||||||||
| 12 | $$GBL{'init'} = 1; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | *Object::InsideOut::dump = sub | ||||||||||||||||||||||||||||
| 15 | 23 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=24 SCAR=32.1
| |||||||||||||||||||||||||||||
| 16 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 23 | my $d_flds = $$GBL{'dump'}{'fld'}; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Extract field info from any :InitArgs hashes | ||||||||||||||||||||||||||||
| 21 | 23 | ▶ | while (my $pkg = shift(@{$$GBL{'dump'}{'args'}})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 8 | my $p_args = $$GBL{'args'}{$pkg}; | |||||||||||||||||||||||||||
| 23 | 8 | foreach my $name (keys(%{$p_args})) { | |||||||||||||||||||||||||||
| 24 | 28 | my $val = $$p_args{$name}; | |||||||||||||||||||||||||||
| 25 | 28 | ▶ | next if (ref($val) ne 'HASH'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 15 | ▶ | if (my $field = $$val{'_F'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 14 | ▶ | $$d_flds{$pkg} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$d_flds->{$pkg} ||= {}
MC/DC100%1/1
$d_flds->{$pkg} ||= {}
$d_flds->{$pkg}
Truth table100%2/2
A
A= $d_flds->{$pkg}
| |||||||||||||||||||||||||||||
| 28 | 14 | ▶ | if (add_dump_field('InitArgs', $name, $field, $$d_flds{$pkg}) eq 'conflict') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | OIO::Code->die( | |||||||||||||||||||||||||||
| 30 | 'message' => 'Cannot dump object', | ||||||||||||||||||||||||||||
| 31 | 'Info' => "In class '$pkg', '$name' refers to two different fields set by 'InitArgs' and '$$d_flds{$pkg}{$name}{'src'}'"); | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Must call ->dump() as an object method | ||||||||||||||||||||||||||||
| 38 | 23 | ▶ | if (! Scalar::Util::blessed($self)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | OIO::Method->die('message' => q/'dump' called as a class method/); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Gather data from the object's class tree | ||||||||||||||||||||||||||||
| 43 | 23 | my %dump; | |||||||||||||||||||||||||||
| 44 | 23 | my $fld_refs = $$GBL{'fld'}{'ref'}; | |||||||||||||||||||||||||||
| 45 | 23 | my $dumpers = $$GBL{'dump'}{'dumper'}; | |||||||||||||||||||||||||||
| 46 | 23 | my $weak = $$GBL{'fld'}{'weak'}; | |||||||||||||||||||||||||||
| 47 | 23 | foreach my $pkg (@{$$GBL{'tree'}{'td'}{ref($self)}}) { | |||||||||||||||||||||||||||
| 48 | # Try to use a class-supplied dumper | ||||||||||||||||||||||||||||
| 49 | 51 | ▶ | if (my $dumper = $$dumpers{$pkg}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 50 | 4 | local $SIG{'__DIE__'} = 'OIO::trap'; | |||||||||||||||||||||||||||
| 51 | 4 | $dump{$pkg} = $self->$dumper(); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | } elsif ($$fld_refs{$pkg}) { | ||||||||||||||||||||||||||||
| 54 | # Dump the data ourselves from all known class fields | ||||||||||||||||||||||||||||
| 55 | 43 | my @fields = @{$$fld_refs{$pkg}}; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Fields for which we have names | ||||||||||||||||||||||||||||
| 58 | 43 | foreach my $name (keys(%{$$d_flds{$pkg}})) { | |||||||||||||||||||||||||||
| 59 | 71 | my $field = $$d_flds{$pkg}{$name}{'fld'}; | |||||||||||||||||||||||||||
| 60 | 71 | ▶ | if (ref($field) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 33 | ▶ | if (exists($$field{$$self})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 24 | $dump{$pkg}{$name} = $$field{$$self}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 38 | ▶ | if (defined($$field[$$self])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 32 | $dump{$pkg}{$name} = $$field[$$self]; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 71 | ▶ | if ($$weak{$field} && exists($dump{$pkg}{$name})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$weak->{$field} and exists $dump{$pkg}{$name}
MC/DC50%1/2
$weak->{$field} and exists $dump{$pkg}{$name}
$weak->{$field}
exists $dump{$pkg}{$name}
Truth table66%2/3
A and B
A= $weak->{$field}B= exists $dump{$pkg}{$name}
| |||||||||||||||||||||||||||||
| 70 | 1 | Scalar::Util::weaken($dump{$pkg}{$name}); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 71 | @fields = grep { $_ != $field } @fields; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Fields for which names are not known | ||||||||||||||||||||||||||||
| 76 | 43 | foreach my $field (@fields) { | |||||||||||||||||||||||||||
| 77 | 5 | ▶ | if (ref($field) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | if (exists($$field{$$self})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 2 | $dump{$pkg}{$field} = $$field{$$self}; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 3 | ▶ | if (defined($$field[$$self])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 3 | $dump{$pkg}{$field} = $$field[$$self]; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 5 | ▶ | if ($$weak{$field} && exists($dump{$pkg}{$field})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$weak->{$field} and exists $dump{$pkg}{$field}
MC/DC0%0/2
$weak->{$field} and exists $dump{$pkg}{$field}
$weak->{$field}
exists $dump{$pkg}{$field}
Truth table33%1/3
A and B
A= $weak->{$field}B= exists $dump{$pkg}{$field}
| |||||||||||||||||||||||||||||
| 87 | 0 | Scalar::Util::weaken($dump{$pkg}{$field}); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Package up the object's class and its data | ||||||||||||||||||||||||||||
| 94 | 23 | my $output = [ ref($self), \%dump ]; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Create a string version of dumped data if arg is true | ||||||||||||||||||||||||||||
| 97 | 23 | ▶ | if ($_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 8 | require Data::Dumper; | |||||||||||||||||||||||||||
| 99 | 8 | local $Data::Dumper::Indent = 1; | |||||||||||||||||||||||||||
| 100 | 8 | $output = Data::Dumper::Dumper($output); | |||||||||||||||||||||||||||
| 101 | 8 | chomp($output); | |||||||||||||||||||||||||||
| 102 | 8 | $output =~ s/^\$VAR1 = //; # Remove leading '$VAR1 = ' | |||||||||||||||||||||||||||
| 103 | 8 | $output =~ s/;$//s; # Remove trailing semi-colon | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Done - send back the dumped data | ||||||||||||||||||||||||||||
| 107 | 23 | return ($output); | |||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | *Object::InsideOut::pump = sub | ||||||||||||||||||||||||||||
| 112 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=18 SCAR=33.7
| |||||||||||||||||||||||||||||
| 113 | 9 | my $input = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Check usage | ||||||||||||||||||||||||||||
| 116 | 9 | ▶ | if ($input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 9 | ▶ | if ($input eq 'Object::InsideOut') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 118 | 5 | $input = shift; # Called as a class method | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | } elsif (Scalar::Util::blessed($input)) { | ||||||||||||||||||||||||||||
| 121 | 0 | OIO::Method->die('message' => q/'pump' called as an object method/); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # Must have an arg | ||||||||||||||||||||||||||||
| 126 | 9 | ▶ | if (! $input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | OIO::Args->die('message' => 'Missing argument to pump()'); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Convert string input to array ref, if needed | ||||||||||||||||||||||||||||
| 131 | 9 | ▶ | if (! ref($input)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 1 | my @errs; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | local $SIG{'__WARN__'} = sub { push(@errs, @_); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | my $array_ref; | |||||||||||||||||||||||||||
| 136 | 1 | eval "\$array_ref = $input"; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | ▶ | if ($@ || @errs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or @errs
MC/DC0%0/2
$@ or @errs
$@
@errs
Truth table33%1/3
A or B
A= $@B= @errs
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my ($err) = split(/ at /, $@ || join(" | ", @errs)); | ||||||||||||||||||||||||||
|
Condition0%0/3
$@ || join(' | ', @errs)
MC/DC0%0/2
$@ || join(' | ', @errs)
$@
join(' | ', @errs)
Truth table0%0/3
A || B
A= $@B= join(' | ', @errs)
| |||||||||||||||||||||||||||||
| 140 | 0 | OIO::Args->die( | |||||||||||||||||||||||||||
| 141 | 'message' => 'Failure converting dump string back to hash ref', | ||||||||||||||||||||||||||||
| 142 | 'Error' => $err, | ||||||||||||||||||||||||||||
| 143 | 'Arg' => $input); | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 1 | $input = $array_ref; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Check input | ||||||||||||||||||||||||||||
| 150 | 9 | ▶ | if (ref($input) ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | OIO::Args->die('message' => 'Argument to pump() is not an array ref'); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # Extract class name and object data | ||||||||||||||||||||||||||||
| 155 | 9 | my ($class, $dump) = @{$input}; | |||||||||||||||||||||||||||
| 156 | 9 | ▶ | if (! defined($class) || ref($dump) ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $class or ref $dump ne "HASH"
MC/DC0%0/2
not defined $class or ref $dump ne "HASH"
not defined $class
ref $dump ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined $classB= ref $dump ne "HASH"
| |||||||||||||||||||||||||||||
| 157 | 0 | OIO::Args->die('message' => 'Argument to pump() is invalid'); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # Create a new 'bare' object | ||||||||||||||||||||||||||||
| 161 | 9 | my $self = _obj($class); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # Store object data | ||||||||||||||||||||||||||||
| 164 | 9 | foreach my $pkg (keys(%{$dump})) { | |||||||||||||||||||||||||||
| 165 | 13 | ▶ | if (! exists($$GBL{'tree'}{'td'}{$pkg})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | OIO::Args->die('message' => "Unknown class: $pkg"); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 13 | my $data = $$dump{$pkg}; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # Try to use a class-supplied pumper | ||||||||||||||||||||||||||||
| 171 | 13 | ▶ | if (my $pumper = $$GBL{'dump'}{'pumper'}{$pkg}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 2 | local $SIG{'__DIE__'} = 'OIO::trap'; | |||||||||||||||||||||||||||
| 173 | 2 | $self->$pumper($data); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | } else { | ||||||||||||||||||||||||||||
| 176 | # Pump in the data ourselves | ||||||||||||||||||||||||||||
| 177 | 11 | foreach my $fld_name (keys(%{$data})) { | |||||||||||||||||||||||||||
| 178 | 18 | my $value = $$data{$fld_name}; | |||||||||||||||||||||||||||
| 179 | 18 | ▶ | if (my $field = $$GBL{'dump'}{'fld'}{$pkg}{$fld_name}{'fld'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 16 | $self->set($field, $value); | |||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||
| 182 | 2 | ▶ | if ($fld_name =~ /^(?:HASH|ARRAY)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 2 | OIO::Args->die( | |||||||||||||||||||||||||||
| 184 | 'message' => "Unnamed field encounted in class '$pkg'", | ||||||||||||||||||||||||||||
| 185 | 'Arg' => "$fld_name => $value"); | ||||||||||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||||||||||
| 187 | 0 | OIO::Args->die( | |||||||||||||||||||||||||||
| 188 | 'message' => "Unknown field name for class '$pkg': $fld_name"); | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # Done - return the object | ||||||||||||||||||||||||||||
| 196 | 7 | return ($self); | |||||||||||||||||||||||||||
| 197 | }; | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # Do the original call | ||||||||||||||||||||||||||||
| 201 | @_ = @args; | ||||||||||||||||||||||||||||
| 202 | goto &$call; | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | } # End of package's lexical scope | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | # Ensure correct versioning | ||||||||||||||||||||||||||||
| 209 | 6 | ▶ | ($Object::InsideOut::VERSION eq '4.05') | ||||||||||||||||||||||||||
|
Condition50%1/2
$VERSION eq '4.05' || die("Version mismatch\n")
MC/DC0%0/1
$VERSION eq '4.05' || die("Version mismatch\n")
$VERSION eq '4.05'
Truth table50%1/2
A
A= $VERSION eq '4.05'
| |||||||||||||||||||||||||||||
| 210 | or die("Version mismatch\n"); | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # EOF | ||||||||||||||||||||||||||||