602
|
1 |
# Copyright (c) 1995-98 Gurusamy Sarathy. All rights reserved.
|
|
2 |
#
|
|
3 |
# Copyright (c) 1998 Raphael Manfredi.
|
|
4 |
#
|
|
5 |
# This program is free software; you can redistribute it and/or
|
|
6 |
# modify it under the same terms as Perl itself.
|
|
7 |
|
|
8 |
package MLDBM::Serializer::Data::Dumper;
|
|
9 |
BEGIN { @MLDBM::Serializer::Data::Dumper::ISA = qw(MLDBM::Serializer) }
|
|
10 |
|
|
11 |
use Data::Dumper '2.08'; # Backward compatibility
|
|
12 |
use Carp;
|
|
13 |
|
|
14 |
#
|
|
15 |
# Create a Data::Dumper serializer object.
|
|
16 |
#
|
|
17 |
sub new {
|
|
18 |
my $self = shift->SUPER::new();
|
|
19 |
my $meth = shift || "";
|
|
20 |
$meth = (defined(&Data::Dumper::Dumpxs) ? 'Dumpxs' : 'Dump')
|
|
21 |
unless $meth =~ /^Dump(xs)?$/;
|
|
22 |
$self->DumpMeth($meth);
|
|
23 |
$self->RemoveTaint(shift);
|
|
24 |
$self->Key(shift);
|
|
25 |
$self;
|
|
26 |
}
|
|
27 |
|
|
28 |
#
|
|
29 |
# Serialize $val if it is a reference, or if it does begin with our magic
|
|
30 |
# key string, since then at retrieval time we expect a Data::Dumper string.
|
|
31 |
# Otherwise, return the scalar value.
|
|
32 |
#
|
|
33 |
sub serialize {
|
|
34 |
my $self = shift;
|
|
35 |
my ($val) = @_;
|
|
36 |
return undef unless defined $val;
|
|
37 |
return $val unless ref($val) or $val =~ m|^\Q$self->{'key'}|o;
|
|
38 |
my $dumpmeth = $self->{'dumpmeth'};
|
|
39 |
local $Data::Dumper::Indent = 0;
|
|
40 |
local $Data::Dumper::Purity = 1;
|
|
41 |
local $Data::Dumper::Terse = 1;
|
|
42 |
return $self->{'key'} . Data::Dumper->$dumpmeth([$val], ['M']);
|
|
43 |
}
|
|
44 |
|
|
45 |
#
|
|
46 |
# If the value is undefined or does not begin with our magic key string,
|
|
47 |
# return it as-is. Otherwise, we need to recover the underlying data structure.
|
|
48 |
#
|
|
49 |
sub deserialize {
|
|
50 |
my $self = shift;
|
|
51 |
my ($val) = @_;
|
|
52 |
return undef unless defined $val;
|
|
53 |
return $val unless $val =~ s|^\Q$self->{'key'}||o;
|
|
54 |
my $M = "";
|
|
55 |
($val) = $val =~ /^(.*)$/s if $self->{'removetaint'};
|
|
56 |
# Disambiguate hashref (perl may treat it as a block)
|
|
57 |
my $N = eval($val =~ /^\{/ ? '+'.$val : $val);
|
|
58 |
return $M ? $M : $N unless $@;
|
|
59 |
carp "MLDBM error: $@\twhile evaluating:\n $val";
|
|
60 |
}
|
|
61 |
|
|
62 |
sub DumpMeth { my $s = shift; $s->_attrib('dumpmeth', @_); }
|
|
63 |
sub RemoveTaint { my $s = shift; $s->_attrib('removetaint', @_); }
|
|
64 |
sub Key { my $s = shift; $s->_attrib('key', @_); }
|
|
65 |
|
|
66 |
# avoid used only once warnings
|
|
67 |
{
|
|
68 |
local $Data::Dumper::Terse;
|
|
69 |
}
|
|
70 |
|
|
71 |
1;
|
|
72 |
__END__
|
|
73 |
|
|
74 |
=head1 AUTHORS
|
|
75 |
|
|
76 |
Gurusamy Sarathy <F<gsar@umich.edu>>.
|
|
77 |
|
|
78 |
Support for multiple serializing packages by
|
|
79 |
Raphael Manfredi <F<Raphael_Manfredi@grenoble.hp.com>>.
|
|
80 |
|
|
81 |
Copyright (c) 1995-98 Gurusamy Sarathy. All rights reserved.
|
|
82 |
|
|
83 |
Copyright (c) 1998 Raphael Manfredi.
|
|
84 |
|
|
85 |
This program is free software; you can redistribute it and/or
|
|
86 |
modify it under the same terms as Perl itself.
|
|
87 |
|
|
88 |
=cut
|