How can I control the formatting of Data::Dumper&#

2019-04-29 03:51发布

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?

7条回答
smile是对你的礼貌
2楼-- · 2019-04-29 04:38

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.

查看更多
神经病院院长
3楼-- · 2019-04-29 04:38

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);
}
查看更多
孤傲高冷的网名
4楼-- · 2019-04-29 04:40

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.

查看更多
叼着烟拽天下
5楼-- · 2019-04-29 04:44

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. :(

查看更多
Ridiculous、
6楼-- · 2019-04-29 04:44

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

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

查看更多
Explosion°爆炸
7楼-- · 2019-04-29 04:48

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)

查看更多
登录 后发表回答