develooper Front page | perl.perl5.porters | Postings from January 2011

Re: [perl #82948] Data::Dumper error repeatly serializes

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
January 31, 2011 03:18
Subject:
Re: [perl #82948] Data::Dumper error repeatly serializes
Message ID:
20110131111757.GY2835@iabyn.com
On Fri, Jan 28, 2011 at 01:28:53AM -0800, unera@debian.org wrote:
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> use utf8;
> use open qw(:std :utf8);
> 
> use Test::More tests => 5;
> 
> 
> BEGIN {
>     use_ok 'Data::Dumper';
> }
> 
> use Data::Dumper;
> my $test = {
>     array   => [ qw( a b c ) ],
>     hash    => { a => 'b', c => 'd' },
>     regexp  => qr/^(a|b|c)$/,
> };
> 
> local $Data::Dumper::Terse = 1;
> local $Data::Dumper::Useqq = 1;
> local $Data::Dumper::Indent = 0;
> 
> 
> my $dump = Dumper($test);
> 
> ok $dump, 'Dump';
> 
> my $o = eval $dump;
> 
> ok !$@, 'Eval';
> 
> my $dump2 = Dumper($o);
> 
> ok $dump2, 'Dump after Eval';
> 
> my $eq_res = ok $dump eq $dump2, 'Dumps are equal';
> diag "$dump\n$dump2" unless $eq_res;
> 
> =cut
> 
> We can't receive the same dump if we will
> serialize-deserialize-serialize using Data::Dumper.

Data::Dumper makes no guarantees that the result of repeated
serialisations will result in the same string; only that they will
be functionally equivalent. What you are seeing is mostly the result of
hash keys being unordered, and thus appearing in differing orders in
different serialisations. If this bothers you, then you could use

    local $Data::Dumper::Sortkeys = 1;

The only thing that differs then is the regexp serialisation, which
accrues multiple sets of flags. This is ugly, but still functionally
equivalent; although I guess we should fix that just for prettiness sake:

        qr/^(a|b|c)$/
becomes qr/(?-xism:^(a|b|c)$)/
becomes qr/(?-xism:(?-xism:^(a|b|c)$))/
....

and similar with (?^ in 5.13.x.


-- 
"You're so sadly neglected, and often ignored.
A poor second to Belgium, When going abroad."
    -- Monty Python, "Finland"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About