Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl hash Data::Dumper

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?

like image 732
Ωmega Avatar asked Mar 13 '12 16:03

Ωmega


People also ask

How to get the same dumper output from different runs of Perl?

If you need to have identical Data::Dumper outputs from different runs of Perl, use the environment variable PERL_HASH_SEED, see PERL_HASH_SEED in perlrun. Using this restores the old (platform-specific) ordering: an even prettier solution might be to use the Sortkeys filter of Data::Dumper.

Why do different runs of Perl have different hash keys?

Starting from Perl 5.8.1 different runs of Perl will have different ordering of hash keys. The change was done for greater security, see Algorithmic Complexity Attacks in perlsec. This means that different runs of Perl will have different Data::Dumper outputs if the data contains hashes.

What is a multidimensional hash in Perl?

Among all of the Perl’s nested structures, a Multidimensional hash or Hash of Hashes is the most flexible. It’s like building up a record that itself contains a group of other records.

How to install Perl datadumper on Linux?

You can install Perl DataDumper by running sudo yum install. how do i install perl libraries on linux? how do i check if a perl module is installed on linux?


1 Answers

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.

like image 187
mob Avatar answered Sep 28 '22 04:09

mob