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?

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
aartist
  • 3,145
  • 3
  • 33
  • 31
  • 3
    What exactly is it you don't like about Data::Dumper's output? – innaM Oct 20 '09 at 15:47
  • 3
    Echoing Manni: You should read the docs and point out what other improvements you need than those that can be achieved by tweaking parameters. – Sinan Ünür Oct 20 '09 at 15:54
  • 1
    I hope you're not using this for production, because that would be the only reason I can see that you would want to make it prettier. It shows the data structure. It's not supposed to be pretty. – Elizabeth Buckwalter Oct 20 '09 at 22:22
  • @Elizabeth: It shouldn't be prettier in the sense of 'dressed up just for the sake of aesthetics', but pretty printing is also about clarity. Clearer output can be genuinely helpful when working with complex data. This is a big part of the reason that `Data::Dumper` and company do pretty printing and offer various formatting options. – Telemachus Oct 21 '09 at 01:25
  • There are tons of options to Data::Dumper. See [the documentation](http://search.cpan.org/perldoc/Data::Dumper#Configuration_Variables_or_Methods). You can fix indentation, padding, variable names, depth, key sorting, etc. – JSBձոգչ Oct 20 '09 at 15:48

7 Answers7

21

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

Edit (20120304): I had completely forgotten this question, but it was upvoted today and that jogged my memory. If I had to recommend anything today (3 years later) for pretty printing in Perl, I would certainly go with Data::Printer. From Data::Printer's own Rationale:

Data::Dumper is a fantastic tool, meant to stringify data structures in a way they are suitable for being eval'ed back in.

The thing is, a lot of people keep using it (and similar ones, like Data::Dump) to print data structures and objects on screen for inspection and debugging, and while you can use those modules for that, it doesn't mean mean you should.

This is where Data::Printer comes in. It is meant to do one thing and one thing only: display Perl variables and objects on screen, properly formatted (to be inspected by a human)

Community
  • 1
  • 1
Telemachus
  • 19,459
  • 7
  • 57
  • 79
5

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
  • 53,118
  • 13
  • 86
  • 159
4

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

Massa
  • 8,647
  • 2
  • 25
  • 26
3

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

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

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
lexu
  • 8,766
  • 5
  • 45
  • 63
  • 3
    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 Oct 20 '09 at 16:23
2

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
  • 29,660
  • 2
  • 47
  • 102
2

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.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
jmcnamara
  • 38,196
  • 6
  • 90
  • 108
2

Data::Dumper::Concise is another possibility.

use Data::Dumper::Concise;
warn Dumper($var);

is equivalent to:

use Data::Dumper;
{
  local $Data::Dumper::Terse = 1;
  local $Data::Dumper::Indent = 1;
  local $Data::Dumper::Useqq = 1;
  local $Data::Dumper::Deparse = 1;
  local $Data::Dumper::Quotekeys = 0;
  local $Data::Dumper::Sortkeys = 1;
  warn Dumper($var);
}
Eric Johnson
  • 17,502
  • 10
  • 52
  • 59