http://qs321.pair.com?node_id=378865

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:

warn Data::Dumper->Dump( [$foo, \%this, \@array, \%that], [qw/*foo *that *array *this/] );

Now I can just type:

use Data::Dumper::Simple; warn Dumper($foo, %this, @array, %that);

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.


NAME

Data::Dumper::Simple - Perl extension for dumping variables


SYNOPSIS

  use Data::Dumper::Simple;
  warn Dumper($scalar, @array, %hash);


ABSTRACT

  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.


DESCRIPTION

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.

The Problem

Frequently, we use Data::Dumper to dump out some variables while debugging. When this happens, we often do this:

use Data::Dumper; warn Dumper($foo, $bar, $baz);

And we get simple output like:

$VAR1 = 3; $VAR2 = 2; $VAR3 = 1;

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:

warn Data::Dumper->Dump( [$foo, $bar, $baz], [qw/*foo *bar *baz/] );

This provides much more useful output.

$foo = 3; $bar = 2; $baz = 1;

(There's more control over the output than what I've shown.)

You can even use this to output more complex data structures:

warn Data::Dumper->Dump( [$foo, \@array], [qw/*foo *array/] );

And get something like this:

$foo = 3; @array = ( 8, 'Ovid' );

Unfortunately, this can involve a lot of annoying typing.

warn Data::Dumper->Dump( [$foo, \%this, \@array, \%that], [qw/*foo *that *array *this/] );

You'll also notice a typo in the second array ref which can cause great confusion while debugging.

The Solution

With Data::Dumper::Simple you can do this instead:

use Data::Dumper::Simple. warn Dumper($scalar, @array, %hash);

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):

$scalar = 'Ovid'; @array = ( 'Data', 'Dumper', 'Simple', 'Rocks!' ); %hash = ( 'it' => 'does', 'I' => 'hope', 'at' => 'least' );

Taking a reference to an array or hash is effectively a no-op, but a scalar containing a reference works as expected:

my $foo = { hash => 'ref' }; my @foo = qw/foo bar baz/; warn Dumper ($foo, \@foo);

Produces:

$foo = { 'hash' => 'ref' }; @foo = ( 'foo', 'bar', 'baz' );

This is to ensure that similarly named variables are properly disambiguated in the output.

EXPORT

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.


SEE ALSO


BUGS AND CAVEATS

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:

Dumper($foo, some_sub()); # Bad!

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.


AUTHOR

Curtis ``Ovid'' Poe, <eop_divo_sitruc@yahoo.com>

Reverse the name to email me.


COPYRIGHT AND LICENSE

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.

Cheers,
Ovid

New address of my CGI Course.