|Do you know where your variables are?|
RFC: Data::Dumper::Simpleby Ovid (Cardinal)
|on Jul 31, 2004 at 04:18 UTC||Need Help??|
I've had an itch I've wanted to scratch for a long time. So it's scratched. I'm tired of typing the following when I want named variables in Data::Dumper output:
Now I can just type:
Note that you don't even need to use references. The variables do not flatten into lists.
POD follows. This works as advertised, but I don't have a bundle uploaded yet. If you're interested in this or have suggestions, please let me know.
Update: Data::Dumper::Simple is now on the CPAN.
Data::Dumper::Simple - Perl extension for dumping variables
use Data::Dumper::Simple; warn Dumper($scalar, @array, %hash);
This module allow the user to dump variables in a Data::Dumper format. Unlike the default behavior of Data::Dumper, the variables are named (instead of $VAR1, $VAR2, etc.) Data::Dumper provides an extended interface that allows the programmer to name the variables, but this interface requires a lot of typing and is prone to tyops (sic). This module fixes that.
Data::Dumper::Simple is actually a source filter that replaces all instances of Dumper($some, @args) in your code with a call to Data::Dumper->Dump(). You can use the one function provided to make dumping variables for debugging a trivial task.
Frequently, we use Data::Dumper to dump out some variables while debugging. When this happens, we often do this:
And we get simple output like:
While this is usually what we want, this can be confusing if we forget which variable corresponds to which variable printed. To get around this, there is an extended interface to Data::Dumper:
This provides much more useful output.
(There's more control over the output than what I've shown.)
You can even use this to output more complex data structures:
And get something like this:
Unfortunately, this can involve a lot of annoying typing.
You'll also notice a typo in the second array ref which can cause great confusion while debugging.
With Data::Dumper::Simple you can do this instead:
Note that there's no need to even take a reference to the variables. The output of the above resembles this (sample data, of course):
Taking a reference to an array or hash is effectively a no-op, but a scalar containing a reference works as expected:
This is to ensure that similarly named variables are properly disambiguated in the output.
The only thing exported is the Dumper() function.
Well, actually that's not really true. Nothing is exported. However, a source filter is used to automatically rewrite any apparent calls to Dumper() so that it just Does The Right Thing.
This module uses a source filter. If you don't like that, don't use this.
There are no known bugs but there probably are some as this is Alpha Code. As for limitations, do not try to call Dumper() with a subroutine in the argument list:
The filter gets confused by the parentheses. Your author was going to fix this but it became apparent that there was no way that Dumper() could figure out how to name the return values from the subroutines, thus ensuring further breakage. So don't do that.
Getting really crazy by using multiple enreferencing will confuse things (e.g., \\\\\\$foo), don't do that, either. I might use Text::Balanced at some point to fix this if it's an issue.
Note that this is not a drop-in replacement for Data::Dumper. If you need the power of that module, use it.
Curtis ``Ovid'' Poe, <email@example.com>
Reverse the name to email me.
Copyright 2004 by Curtis ``Ovid'' Poe
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.