good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Reinventing the wheel: Dumper Difficultiesby demerphq (Chancellor) |
on Apr 10, 2002 at 15:19 UTC ( [id://158040]=perlmeditation: print w/replies, xml ) | Need Help?? |
Recently I have been routinely committing that most grave crime of reinventing wheels. The wheels are Data::Dumper and Data::Dump. 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.
Data::Dumper while a tried and true workhorse has a number of problems. Under MS it has a memory leak and can not handle large data structures. But even worse it has a very subtle bug with how it outputs references to scalars. This can be seen by the following very simple example
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. 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. 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. :-)
Yves / DeMerphq
Back to
Meditations
|
|