~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/CPAN/lib/CGI/Session/Serialize/default.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package CGI::Session::Serialize::default;
 
2
 
 
3
# $Id: default.pm 447 2008-11-01 03:46:08Z markstos $ 
 
4
 
 
5
use strict;
 
6
use Safe;
 
7
use Data::Dumper;
 
8
use CGI::Session::ErrorHandler;
 
9
use Scalar::Util qw(blessed reftype refaddr);
 
10
use Carp "croak";
 
11
use vars qw( %overloaded );
 
12
require overload;
 
13
 
 
14
@CGI::Session::Serialize::default::ISA = ( "CGI::Session::ErrorHandler" );
 
15
$CGI::Session::Serialize::default::VERSION = '4.38';
 
16
 
 
17
 
 
18
sub freeze {
 
19
    my ($class, $data) = @_;
 
20
    
 
21
    my $d =
 
22
    new Data::Dumper([$data], ["D"]);
 
23
    $d->Indent( 0 );
 
24
    $d->Purity( 1 );
 
25
    $d->Useqq( 0 );
 
26
    $d->Deepcopy( 0 );
 
27
    $d->Quotekeys( 1 );
 
28
    $d->Terse( 0 );
 
29
    
 
30
    # ;$D added to make certain we get our data structure back when we thaw
 
31
    return $d->Dump() . ';$D';
 
32
}
 
33
 
 
34
sub thaw {
 
35
    my ($class, $string) = @_;
 
36
 
 
37
    # To make -T happy
 
38
     my ($safe_string) = $string =~ m/^(.*)$/s;
 
39
     my $rv = Safe->new->reval( $safe_string );
 
40
    if ( $@ ) {
 
41
        return $class->set_error("thaw(): couldn't thaw. $@");
 
42
    }
 
43
    __walk($rv);
 
44
    return $rv;
 
45
}
 
46
 
 
47
sub __walk {
 
48
    my %seen;
 
49
    my @filter = __scan(shift);
 
50
    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
    while (@filter) {
 
56
                defined(my $x = shift @filter) or next;
 
57
        $seen{refaddr $x || ''}++ and next;
 
58
          
 
59
        my $r = reftype $x or next;
 
60
        if ($r eq "HASH") {
 
61
            # we use this form to make certain we have aliases
 
62
            # to the values in %$x and not copies
 
63
            push @filter, __scan(@{$x}{keys %$x});
 
64
        } elsif ($r eq "ARRAY") {
 
65
            push @filter, __scan(@$x);
 
66
        } elsif ($r eq "SCALAR" || $r eq "REF") {
 
67
            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
sub __scan {
 
76
    # $_ gets aliased to each value from @_ which are aliases of the values in 
 
77
    #  the current data structure
 
78
    for (@_) {
 
79
        if (blessed $_) {
 
80
            if (overload::Overloaded($_)) {
 
81
                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
                if (exists $overloaded{$address}) {
 
86
                    $_ = $overloaded{$address};
 
87
                } else {
 
88
                    my $reftype = reftype $_;                
 
89
                    if ($reftype eq "HASH") {
 
90
                        $_ = $overloaded{$address} = bless { %$_ }, ref $_;
 
91
                    } elsif ($reftype eq "ARRAY") {
 
92
                        $_ = $overloaded{$address} =  bless [ @$_ ], ref $_;
 
93
                    } elsif ($reftype eq "SCALAR" || $reftype eq "REF") {
 
94
                        $_ = $overloaded{$address} =  bless \do{my $o = $$_},ref $_;
 
95
                    } else {
 
96
                        croak "Do not know how to reconstitute blessed object of base type $reftype";
 
97
                    }
 
98
                }
 
99
            } else {
 
100
                bless $_, ref $_;
 
101
            }
 
102
        }
 
103
    }
 
104
    return @_;
 
105
}
 
106
 
 
107
 
 
108
1;
 
109
 
 
110
__END__;
 
111
 
 
112
=pod
 
113
 
 
114
=head1 NAME
 
115
 
 
116
CGI::Session::Serialize::default - Default CGI::Session serializer
 
117
 
 
118
=head1 DESCRIPTION
 
119
 
 
120
This library is used by CGI::Session driver to serialize session data before storing it in disk.
 
121
 
 
122
All the methods are called as class methods.
 
123
 
 
124
=head1 METHODS
 
125
 
 
126
=over 4
 
127
 
 
128
=item freeze($class, \%hash)
 
129
 
 
130
Receives two arguments. First is the class name, the second is the data to be serialized. Should return serialized string on success, undef on failure. Error message should be set using C<set_error()|CGI::Session::ErrorHandler/"set_error()">
 
131
 
 
132
=item thaw($class, $string)
 
133
 
 
134
Received two arguments. First is the class name, second is the I<frozen> data string. Should return thawed data structure on success, undef on failure. Error message should be set using C<set_error()|CGI::Session::ErrorHandler/"set_error()">
 
135
 
 
136
=back
 
137
 
 
138
=head1 LICENSING
 
139
 
 
140
For support and licensing see L<CGI::Session|CGI::Session>
 
141
 
 
142
=cut
 
143