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