Front page | perl.perl5.porters |
Postings from January 2011
Re: [perl #82948] Data::Dumper error repeatly serializes
Thread Previous
From:
demerphq
Date:
January 31, 2011 03:32
Subject:
Re: [perl #82948] Data::Dumper error repeatly serializes
Message ID:
AANLkTikpvqMFwvgb-z_0xYFoXOmWDD+q=ULnOCpxbhqg@mail.gmail.com
On 31 January 2011 12:17, Dave Mitchell <davem@iabyn.com> wrote:
> 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.
Data::Dump::Streamer does this properly, and there is no longer any
reason that DD does not. The routine that DDS uses for this has been
in core in the re namespace for some time:
my ($pat,$mods)=regexp_pattern(qr/foo/i);
So DD could use it to extract the flags and pattern without them being
wrapped in (?...) brackets, and thus round trip regexen properly.
One of the reasons that I wrote DDS was that DD doesn't/didn't handle
certain structures properly, including regexes, and especially
_blessed_ regexes.
Example:
$ perl -MData::Dump::Streamer -e'my ($x,$y); $x=\$y; $y=\$x; print Dump($x,$y)'
$REF1 = \$REF2;
$REF2 = \$REF1;
$ perl -MData::Dumper -e'my ($x,$y); $x=\$y; $y=\$x; print Dumper($x,$y)'
$VAR1 = \\$VAR1;
$VAR2 = ${$VAR1};
Perhaps the OP should try using it instead, although it is MUCH
slower. But also much more accurate.
Alternatively they maybe should investigate using storable.
Yves
--
perl -Mre=debug -e "/just|another|perl|hacker/"
Thread Previous