开发者

I don't want to use Data::Dumper's default settings, what can I do?

I find开发者_如何学编程 myself using this method to print out Perl values all the time:

sub d {
  Data::Dumper->new([])->Terse(1)->Indent(0)->Values([$_[0]])->Dump;
}

say "x = ", d($x), ' y = ', d($y);

I like this because I don't want $VAR1 = in my output, and I rarely deal with recursive data structures.

But the thought of creating a new Data::Dumper object and performing that long chain of initializations every time I call d() bothers me.

Is there another stringifier I can use?


Option 1, use the Data::Dumper variables:

$Data::Dumper::Terse  = 1;
$Data::Dumper::Indent = 0;

say Dumper "x =", Dumper($x), " y = ", Dumper($y);  


sub d {
  use feature 'state';

  state $dd = Data::Dumper->new([])->Terse(1)->Indent(0);
  return $dd->Values(shift)->Dump;
}

Untested, but something like this should work.


I tried Data::Dump and never looked back.

use Data::Dump 'dump';

dump $structure;


Data::Dumper::Concise is handy. Not the same settings as you require, but possibly good for someone else. Sortkeys in particular is essential

From the documentation:

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜