tags:

views:

253

answers:

7

I am using Data::Dumper::Dumper() method. The output is good, but can be made little compact and more good looking.

How I can control it? What are the better alternatives?

+10  A: 

There are tons of options to Data::Dumper. See the documentation. You can fix indentation, padding, variable names, depth, key sorting, etc.

JSBangs
+2  A: 

One alternative* to Data::Dumper would be JSON and its Perl implementation JSON.

* Whether it is better is up to you to decide.

lexu
It's certainly better for cross-platform or cross-language communication, such as preparing a data structure to be received by javascript or a Flash application. There are JSON libraries for most modern languages, and many post-modern and decrepit ones too. :)
Ether
+11  A: 

Take a look at Data::Dump for something similar to Data::Dumper but arguably better at pretty printing.

Telemachus
+4  A: 

If you want to serialize output for storage (rather than for display), take a look at Storable's freeze() and thaw(). I cringe whenever I see Data::Dumper being used to save data structures in a DB or cache. :(

Ether
+2  A: 

I normally use Data::Dump::Streamer, but as the others said, only when the options to Data::Dumper aren't enough.

Massa
A: 

If you're just looking for dump output: Smart::Comments.

You just use it.

use Smart::Commments;

And then you put any simple variable in a three-hash comment, like so:

my $v = black_box_process();
### $v

And it dumps it out in almost the prettiest print possible.

You can also manage more complex expressions like so:

### ( $a && ( $b ^ ( $c || $d ))) : ( $a && ( $b ^ ( $c || $d )))

But you have to watch it for "colon paths".

### $My::Package::variable

or

### %My::Package::

has never worked in my experience. If I want them to work then I need something like this:

my %stash = %My::Package::;
### %stash

It also does a number of other cute tricks, which you can see if you read the documentation.

Axeman
+1  A: 

One option is to use Data::Dumper::Perltidy which is a (more or less) drop-in replacement for Data::Dumper::Dumper() but which uses Perltidy to format the output.

jmcnamara