I can hear the hiss of shock. Dumper!? Why Dumper? Its been a standard module for ever. It was written by G. Sarathy. It's tried and true. (Ditto goes for G. Aas's Dump module)
Well. The first answer is that I dont like their output. Either of them. They do a depth first traversal of their data structures, which while fast and efficient renders cyclic and self referential data structures utterly unrecognizable, and usually completely incomprehensable. What I want (and have written) is a dumper that does a breadth first traversal so that if an object is referenced at multiple places in a data structure I want it to appear in the dump at the highest level it is mentioned, not wherever it gets first encountered in a depth first traversal.
But it turns out that there are even better reasons. Both are buggy.
Do you see the error? Its not obvious but its serious. The bug is that we can do this $$x='Bar'; but if we take the output of Dumper and try the same thing $$VAR1='Bar'; we get a Modification of a read-only value error.use Data::Dumper; my ($x,$y); $y='Foo'; $x=\$y; print Dumper($x); __END__ #Outputs $VAR1=\'Foo';
So the output of Data::Dumper can not be relied on to correctly recreate its input.
And now for Data::Dump. Data::Dump suffers from the same problem with references to scalars as Dumper (hardly suprising as Data::Dump was originally by Sarathy). But it has even more serious problems
will cause Data::Dump to go into an infinite loop, which obviously means that we should be cautious to say the least when using Data::Dump for anything non-trivial.use Data::Dump; my ($x,$y); $x=\$y; $y=\$x; print dump([$x,$y]);
Let this be a warning to those of you that use Data::Dumper or Data::Dump for persistancy purposes (with Storable or MLDBM for example)
So in the process of reinventing the wheel I discovered that the wheel isn't as good as I thought it was in the first place. I wonder how many other modules this applies to? CGI perhaps? Maybe we shouldnt be so harsh on those who think that a little reinventing the wheel isnt a bad thing. You never know we might end up with better wheels in the long run...
BTW, heres how my dumper (Data::BFDump) and Data::Dumper would handle that last example (its a test case that I call "Scalar Cross"), I know which one I would rather try to figure out, and it shows the difference between the results of a depth first traversal and a breadth first traversal.
Watch for the initial release of Data::BFDump on your local CPAN mirror over the next few days.#Dumper Output with Purity on $VAR1 = [ \\do{my $o}, do{my $o} ]; ${${$VAR1->[0]}} = $VAR1->[0]; $VAR1->[1] = ${$VAR1->[0]}; #Standard BFDump output do { my $RT_ARRAY = [ \do { my $t }, \do { my $t } ]; ${$RT_ARRAY->[0]} = $RT_ARRAY->[1]; ${$RT_ARRAY->[1]} = $RT_ARRAY->[0]; $RT_ARRAY }
:-)
Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Reinventing the wheel: Dumper Difficulties
by merlyn (Sage) on Apr 10, 2002 at 15:34 UTC | |
by demerphq (Chancellor) on Apr 10, 2002 at 15:58 UTC | |
|
Re: Reinventing the wheel: Dumper Difficulties
by clintp (Curate) on Apr 11, 2002 at 02:56 UTC | |
by demerphq (Chancellor) on Apr 11, 2002 at 08:54 UTC | |
|
(MeowChow) Re: Reinventing the wheel: Dumper Difficulties
by MeowChow (Vicar) on Apr 11, 2002 at 04:11 UTC | |
by demerphq (Chancellor) on Apr 11, 2002 at 08:44 UTC | |
by jmcnamara (Monsignor) on Apr 11, 2002 at 08:52 UTC |