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?
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.
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.
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With