Data::Dumper
Data::Dumper turns any data structure into readable text, which is perfect for debugging.
In this page:
Data::Dumper
Data::Dumper exports the Dumper function, which prints a structure as Perl code, following references into nested data. Setting $Data::Dumper::Sortkeys = 1 sorts hash keys so the output is stable, and $Data::Dumper::Indent controls the layout. Pass references such as \@array or \%hash to Dumper so that it shows the structure, not a flattened list.
Note:
Set $Data::Dumper::Terse = 1 to drop the $VAR1 = prefix and the trailing semicolon.
Example: Data::Dumper
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
my $data = {
name => "Ada",
langs => ["Perl", "C"],
active => 1,
};
print Dumper($data);
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
print Dumper([1, "two", { three => 3 }]), "\n";
# Output:
# $VAR1 = {
# 'active' => 1,
# 'langs' => [
# 'Perl',
# 'C'
# ],
# 'name' => 'Ada'
# };
# [1,'two',{'three' => 3}]
Login to try C/C++/Java/PHP code in the editor
Common Mistakes
- Passing an array or hash directly so that it is flattened
- Forgetting Sortkeys and getting a different key order each run
- Using Dumper output as a stable data format across Perl versions
Chapter Summary
- Dumper prints structures as Perl code
- Pass references to it
- Sortkeys = 1 gives stable output
- Terse = 1 removes the $VAR1 prefix
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: