File Coverage

blib/lib/MLDBM/Serializer/Data/Dumper.pm
Criterion Covered Total %
statement 37 38 97.3
branch 13 20 65.0
condition 3 5 60.0
subroutine 9 9 100.0
pod 0 6 0.0
total 62 78 79.4


line stmt bran cond sub pod time code
1             ####################################################################
2             package MLDBM::Serializer::Data::Dumper;
3 1     1   38 BEGIN { @MLDBM::Serializer::Data::Dumper::ISA = qw(MLDBM::Serializer) }
4            
5 1     1   7 use Data::Dumper '2.08'; # Backward compatibility
  1         1  
  1         150  
6 1     1   6 use Carp;
  1         2  
  1         515  
7            
8             #
9             # Create a Data::Dumper serializer object.
10             #
11             sub new {
12 1     1 0 9 my $self = shift->SUPER::new();
13 1   50     9 my $meth = shift || "";
14 1 50       10 $meth = (defined(&Data::Dumper::Dumpxs) ? 'Dumpxs' : 'Dump')
    50          
15             unless $meth =~ /^Dump(xs)?$/;
16 1         18 $self->DumpMeth($meth);
17 1         2 $self->RemoveTaint(shift);
18 1         2 $self->Key(shift);
19 1         3 $self;
20             }
21            
22             #
23             # Serialize $val if it is a reference, or if it does begin with our magic
24             # key string, since then at retrieval time we expect a Data::Dumper string.
25             # Otherwise, return the scalar value.
26             #
27             sub serialize {
28 6     6 0 7 my $self = shift;
29 6         6 my ($val) = @_;
30 6 50       14 return undef unless defined $val;
31 6 100 66     55 return $val unless ref($val) or $val =~ m|^\Q$self->{'key'}|o;
32 3         6 my $dumpmeth = $self->{'dumpmeth'};
33 3         6 local $Data::Dumper::Indent = 0;
34 3         4 local $Data::Dumper::Purity = 1;
35 3         4 local $Data::Dumper::Terse = 1;
36 3         29 return $self->{'key'} . Data::Dumper->$dumpmeth([$val], ['M']);
37             }
38            
39             #
40             # If the value is undefined or does not begin with our magic key string,
41             # return it as-is. Otherwise, we need to recover the underlying data structure.
42             #
43             sub deserialize {
44 6     6 0 7 my $self = shift;
45 6         8 my ($val) = @_;
46 6 50       12 return undef unless defined $val;
47 6 100       50 return $val unless $val =~ s|^\Q$self->{'key'}||o;
48 3         4 my $M = "";
49 3 50       7 ($val) = $val =~ /^(.*)$/s if $self->{'removetaint'};
50             # Disambiguate hashref (perl may treat it as a block)
51 3 50       269 my $N = eval($val =~ /^\{/ ? '+'.$val : $val);
52 3 100       24 return $M ? $M : $N unless $@;
    50          
53 0         0 carp "MLDBM error: $@\twhile evaluating:\n $val";
54             }
55            
56 1     1 0 1 sub DumpMeth { my $s = shift; $s->_attrib('dumpmeth', @_); }
  1         8  
57 1     1 0 2 sub RemoveTaint { my $s = shift; $s->_attrib('removetaint', @_); }
  1         2  
58 1     1 0 2 sub Key { my $s = shift; $s->_attrib('key', @_); }
  1         3  
59            
60             # avoid used only once warnings
61             {
62             local $Data::Dumper::Terse;
63             }
64            
65             1;