MxScreen/Serializer.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 82 CC 7 SCAR 23.0
CC
7
Cov
0%
serialize
20.0
deserialize
20.0
make
2.0
CRAP 10.0
1 #
2 # $Id: Serializer.pm,v 0.1 2001/04/22 17:57:03 ram Exp $
3 #
4 # Copyright (c) 1998-2001, Raphael Manfredi
5 # Copyright (c) 2000-2001, Christophe Dehaudt
6 #
7 # You may redistribute only under the terms of the Artistic License,
8 # as specified in the README file that comes with the distribution.
9 #
10 # HISTORY
11 # $Log: Serializer.pm,v $
12 # Revision 0.1 2001/04/22 17:57:03 ram
13 # Baseline for first Alpha release.
14 #
15 # $EndLog$
16 #
17  
18 use strict;
19  
20 package CGI::MxScreen::Serializer;
21  
22 use Carp::Datum;
23 use Log::Agent;
24  
25 #
26 # ->make
27 #
28 # Creation routine.
29 #
30 sub make { logconfess "deferred" }
31  
32 #
33 # ->_init
34 #
35 # Records freezer, thawer and compression parameters.
36 #
37 sub _init {
38 DFEATURE my $f_;
39 my $self = shift;
40 my ($freeze, $thaw, $compress) = @_;
41  
42 $self->{freezer} = $freeze;
43 $self->{thawer} = $thaw;
44 $self->{compress} = $compress;
45 }
46  
47 #
48 # Attributes
49 #
50  
51 sub freezer { $_[0]->{freezer} }
52 sub thawer { $_[0]->{thawer} }
53 sub compress { $_[0]->{compress} }
54  
55 #
56 # ->serialize
57 #
58 # Serialize reference.
59 # Returns frozen string, undef on error.
60 #
61 sub serialize {
62 DFEATURE my $f_;
63 my $self = shift;
64 my ($ref) = @_;
65  
66 DREQUIRE ref $ref, "serializing a reference";
67  
68 my $frozen;
69 eval {
70 $frozen = &{$self->freezer}($ref);
71 };
72 logerr "unable to serialize $ref: $@" if chop $@;
73 return DVAL undef unless defined $frozen;
74  
75 if ($self->compress) {
76 require Compress::Zlib;
77 $frozen = Compress::Zlib::compress($frozen);
78 }
79  
80 return DVAL $frozen;
81 }
82  
83 #
84 # ->deserialize
85 #
86 # Deserialize frozen string, passed as reference.
87 # Returns ref to thawed data.
88 #
89 sub deserialize {
90 my $self = shift;
91 my ($frozen) = @_;
92  
93 DREQUIRE !ref $frozen, "deserializing a scalar";
94  
95 if ($self->compress) {
96 require Compress::Zlib;
97 $frozen = Compress::Zlib::uncompress($frozen);
98 }
99  
100 my $data;
101 eval {
102 $data = &{$self->thawer}($frozen);
103 };
104 logerr "unable to deserialize $frozen: $@" if chop $@;
105 return DVAL undef unless defined $data;
106  
107 DENSURE ref $data, "data returned as reference";
108  
109 return DVAL $data;
110 }
111  
112 1;
113  
114 =head1 NAME
115  
116 CGI::MxScreen::Serializer - Abstract serializer interface
117  
118 =head1 SYNOPSIS
119  
120 # Deferred class, only heirs may be instantiated
121  
122 # In heirs:
123 use base qw(CGI::MxScreen::Serializer);
124  
125 sub make { # define the creation routine
126 ...
127 $self->_init(\&freeze, \&thaw, $compress);
128 }
129  
130 # Access interface - $ser is an instantiated heir
131 my $serialized = $ser->serialize($ref);
132 my $deserialized = $ser->deserialize($serialized);
133  
134 =head1 DESCRIPTION
135  
136 This module implements a serializer abstraction and should probably be
137 a CPAN module of its own. I'm seriously thinking about it.
138  
139 The C<CGI::MxScreen::Serializer> class is B<deferred>. The only thing
140 it lacks is a creation routine, which will initialize the
141 C<freeze> and C<thaw> attributes with code references to the proper
142 freezing and thawing routines.
143  
144 It bluntly assumes those routine will conform to the following signatures:
145  
146 freeze(x: REF): STRING
147 thaw(x: STRING): REF
148  
149 and satisfy the following condition, for every I<x>:
150  
151 thaw(freeze(x)).deep_equal(x)
152  
153 where I<deep_equal> is an operation testing that its arguments are
154 structurally equivalent, whatever that means.
155  
156 This class also supports on-the-fly compression and decompression of the
157 serialized data via C<Compress::Zlib>.
158  
159 =head1 SUPPORTED SERIALIZERS
160  
161 Currently, the only serializer available is
162 C<CGI::MxScreen::Serializer::Storable> which is simply installing
163 C<Storable>.
164  
165 =head1 INTERFACE
166  
167 =head2 Attributes
168  
169 The following read-only attributes are defined. The are meant to be
170 initialized at creation time via C<_init()>:
171  
172 =over 4
173  
174 =item C<compress>
175  
176 A boolean flag, stating whether C<Compress::Zlib> compression and decompression
177 should be performed.
178  
179 =item C<freezer>
180  
181 A CODE reference on the freezing routine.
182  
183 =item C<thawer>
184  
185 A CODE reference on the thawing routine.
186  
187 =back
188  
189 =head2 Routines
190  
191 =over 4
192  
193 =item C<_init> I<freezer>, I<thawer>, I<compress_flag>
194  
195 This routine must be called by the C<make()> routine in heirs to initialize
196 the attributes.
197  
198 =item C<deserialize> I<frozen>
199  
200 Deserialize the I<frozen> serialized string generated by C<serialize()>
201 and return a reference to its root object.
202  
203 =item C<serialize> I<reference>
204  
205 Serialize the I<reference> and return a string that can be deserialized
206 by C<deserialize>.
207  
208 =back
209  
210 =head1 AUTHOR
211  
212 Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
213  
214 =head1 SEE ALSO
215  
216 CGI::MxScreen::Serializer::Storable(3).
217  
218 =cut
219