| 1 | package CGI::Session::Serialize::default; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # $Id$ | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 21 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 21 | ▶ | use Safe; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 21 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 21 | ▶ | use CGI::Session::ErrorHandler; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 21 | ▶ | use Scalar::Util qw(blessed reftype refaddr); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 21 | ▶ | use Carp "croak"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 21 | ▶ | use vars qw( %overloaded ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 21 | require overload; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 21 | @CGI::Session::Serialize::default::ISA = ( "CGI::Session::ErrorHandler" ); | |||||||||||||||||||
| 15 | 21 | $CGI::Session::Serialize::default::VERSION = '4.43'; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 27 | ▶ | sub freeze { | ||||||||||||||||||
|
Subroutine
freeze: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 19 | 27 | my ($class, $data) = @_; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 27 | my $d = | |||||||||||||||||||
| 22 | new Data::Dumper([$data], ["D"]); | ||||||||||||||||||||
| 23 | 27 | $d->Indent( 0 ); | |||||||||||||||||||
| 24 | 27 | $d->Purity( 1 ); | |||||||||||||||||||
| 25 | 27 | $d->Useqq( 0 ); | |||||||||||||||||||
| 26 | 27 | $d->Deepcopy( 0 ); | |||||||||||||||||||
| 27 | 27 | $d->Quotekeys( 1 ); | |||||||||||||||||||
| 28 | 27 | $d->Terse( 0 ); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # ;$D added to make certain we get our data structure back when we thaw | ||||||||||||||||||||
| 31 | 27 | return $d->Dump() . ';$D'; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 13 | ▶ | sub thaw { | ||||||||||||||||||
|
Subroutine
thaw: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 35 | 13 | my ($class, $string) = @_; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # To make -T happy | ||||||||||||||||||||
| 38 | 13 | my ($safe_string) = $string =~ m/^(.*)$/s; | |||||||||||||||||||
| 39 | 13 | my $rv = Safe->new->reval( $safe_string ); | |||||||||||||||||||
| 40 | 13 | ▶ | if ( $@ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | return $class->set_error("thaw(): couldn't thaw. $@"); | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | 13 | __walk($rv); | |||||||||||||||||||
| 44 | 13 | return $rv; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 13 | ▶ | sub __walk { | ||||||||||||||||||
|
Subroutine
__walk: called CC=10 SCAR=24.1
| |||||||||||||||||||||
| 48 | 13 | my %seen; | |||||||||||||||||||
| 49 | 13 | my @filter = __scan(shift); | |||||||||||||||||||
| 50 | 13 | local %overloaded; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | # We allow the value assigned to a key to be undef. | ||||||||||||||||||||
| 53 | # Hence the defined() test is not in the while(). | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 13 | ▶ | while (@filter) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 138 | ▶ | defined(my $x = shift @filter) or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 136 | ▶ | $seen{refaddr $x || ''}++ and next; | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
builtin::refaddr($x) || ''
MC/DC100%1/1
builtin::refaddr($x) || ''
builtin::refaddr($x)
Truth table100%2/2
A
A= builtin::refaddr($x)
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 45 | ▶ | my $r = reftype $x or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 32 | ▶ | if ($r eq "HASH") { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 61 | # we use this form to make certain we have aliases | ||||||||||||||||||||
| 62 | # to the values in %$x and not copies | ||||||||||||||||||||
| 63 | 23 | push @filter, __scan(@{$x}{keys %$x}); | |||||||||||||||||||
| 64 | } elsif ($r eq "ARRAY") { | ||||||||||||||||||||
| 65 | 9 | push @filter, __scan(@$x); | |||||||||||||||||||
| 66 | ▶ | } elsif ($r eq "SCALAR" || $r eq "REF") { | |||||||||||||||||||
|
Condition0%0/3
$r eq "SCALAR" or $r eq "REF"
MC/DC0%0/2
$r eq "SCALAR" or $r eq "REF"
$r eq "SCALAR"
$r eq "REF"
Truth table0%0/3
A or B
A= $r eq "SCALAR"B= $r eq "REF"
| |||||||||||||||||||||
| 67 | 0 | push @filter, __scan($$x); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # we need to do this because the values we get back from the safe compartment | ||||||||||||||||||||
| 73 | # will have packages defined from the safe compartment's *main instead of | ||||||||||||||||||||
| 74 | # the one we use | ||||||||||||||||||||
| 75 | 45 | ▶ | sub __scan { | ||||||||||||||||||
|
Subroutine
__scan: called CC=9 SCAR=26.5
| |||||||||||||||||||||
| 76 | # $_ gets aliased to each value from @_ which are aliases of the values in | ||||||||||||||||||||
| 77 | # the current data structure | ||||||||||||||||||||
| 78 | 45 | for (@_) { | |||||||||||||||||||
| 79 | 138 | ▶ | if (blessed $_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 7 | ▶ | if (overload::Overloaded($_)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 3 | my $address = refaddr $_; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | # if we already rebuilt and reblessed this item, use the cached | ||||||||||||||||||||
| 84 | # copy so our ds is consistent with the one we serialized | ||||||||||||||||||||
| 85 | 3 | ▶ | if (exists $overloaded{$address}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 1 | $_ = $overloaded{$address}; | |||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||
| 88 | 2 | my $reftype = reftype $_; | |||||||||||||||||||
| 89 | 2 | ▶ | if ($reftype eq "HASH") { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 90 | 2 | $_ = $overloaded{$address} = bless { %$_ }, ref $_; | |||||||||||||||||||
| 91 | } elsif ($reftype eq "ARRAY") { | ||||||||||||||||||||
| 92 | 0 | $_ = $overloaded{$address} = bless [ @$_ ], ref $_; | |||||||||||||||||||
| 93 | ▶ | } elsif ($reftype eq "SCALAR" || $reftype eq "REF") { | |||||||||||||||||||
|
Condition0%0/3
$reftype eq "SCALAR" or $reftype eq "REF"
MC/DC0%0/2
$reftype eq "SCALAR" or $reftype eq "REF"
$reftype eq "SCALAR"
$reftype eq "REF"
Truth table0%0/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq "REF"
| |||||||||||||||||||||
| 94 | 0 | $_ = $overloaded{$address} = bless \do{my $o = $$_},ref $_; | |||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||
| 96 | 0 | croak "Do not know how to reconstitute blessed object of base type $reftype"; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||
| 100 | 4 | bless $_, ref $_; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | 45 | return @_; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 21 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | __END__; | ||||||||||||||||||||