12

In Perl I need to analyze a huge hash, so I print it into a file using Data::Dumper module. Because it is a huge file, it is very hard to read. Is it possible somehow to print Dumper output nicely, so when I will find a string that I am looking for, I will be able to see immediately key structure where the string I am looking for is stored?

Currently I am using just a simple code:

            use Data::Dumper;
            ...
            print Dumper $var;

What is the best syntax or alternative to get nice output?

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • 2
    Why not give an example of the output you get, and the output you want? Dumper output is by default formatted so you can easily see key structure, so what else are you looking for? – TLP Mar 13 '12 at 16:41
  • 2
    -1 You're asking the wrong question. The dump is a huge intertwined object. The question should be what the appropriate API calls are to get the data you want, not how to [prettify a dump](http://p3rl.org/perltidy). – daxim Mar 13 '12 at 17:35

4 Answers4

24

I almost always set

$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;

with Data::Dumper. The first statement makes the output more compact and much more readable when your data structure is several levels deep. The second statement makes it easier to scan the output and quickly find the keys you are most interested in.

If the data structure contains binary data or embedded tabs/newlines, also consider

$Data::Dumper::Useqq = 1;

which will output a suitable readable representation for that data.

Much more in the perldoc.

mob
  • 117,087
  • 18
  • 149
  • 283
  • Sorry, but I'm going to ask what might be a newbie question about this answer. If the strict pragma is in effect, do you have to do some sort of declaration of $Data before the above statements? – Ben Slade Nov 24 '15 at 14:58
  • 2
    No you do not. `$Data::Dumper::Indent` and `$foo::bar` are "qualified" variable names (they specify the package as well as the variable name) and are allowed under `strict`. – mob Nov 24 '15 at 19:33
10

One possible solution is to use Data::Dumper::Perltidy which runs the output of Data::Dump through Perltidy.

#!/usr/bin/perl -w

use strict;
use Data::Dumper::Perltidy;

my $data = [{ title => 'This is a test header' },{ data_range =>
           [ 0, 0, 3, 9] },{ format     => 'bold' }];

print Dumper $data;

__END__

Prints:

$VAR1 = [
    { 'title'      => 'This is a test header' },
    { 'data_range' => [ 0, 0, 3, 9 ] },
    { 'format'     => 'bold' }
];

Another way is to use Data::Dump.

jmcnamara
  • 38,196
  • 6
  • 90
  • 108
0
$Data::Dumper::Sortkeys = 1;

If you want to get a more reliable result then you have to follow the dumper next. Put in the suitable word to operate that function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-1

This answers the question.

my $WWW_Scripter_Plugin_JavaScript_JE = ${ $VAR1->[1]{156192192} };
my $JE_Object_String = ${ $WWW_Scripter_Plugin_JavaScript_JE->{pf}{String} };
my $JE_Object_Function = ${ $JE_Object_String->{props}{search} };
my $REF = ${ $JE_Object_Function->{global} };
my $HTML_DOM_Element_Img = $REF->{classes}{'HTML::DOM::Element::Img'};

It also violates encapsulation. Perl lets you do it, but you should rather ask how to get at the data with the published WWW::Scripter API.

daxim
  • 39,270
  • 4
  • 65
  • 132
  • 1
    There used to be a comment attached to the question pointing to a download of a huge dump of a WWW::Scripter object, that comment was deleted. This answer is a reply to that comment. – daxim Mar 16 '12 at 10:41