2

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?

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
perlman
  • 139
  • 4

4 Answers4

5

Option 1, use the Data::Dumper variables:

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

say Dumper "x =", Dumper($x), " y = ", Dumper($y);  
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
5
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.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
  • Yeah - I've done that. I also clear out `Values` so that the dumper object doesn't keep references to the last thing you dumped. – perlman Jul 27 '11 at 21:40
4

I tried Data::Dump and never looked back.

use Data::Dump 'dump';

dump $structure;
Zaid
  • 36,680
  • 16
  • 86
  • 155
1

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);
}
plusplus
  • 1,992
  • 15
  • 22