← Back to Perl Course | Chapter 8: Modules | Lesson 3 of 7

Data::Dumper

Data::Dumper turns any data structure into readable text, which is perfect for debugging.

In this page:

  1. Data::Dumper

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

perl
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}]
Common Mistakes
  1. Passing an array or hash directly so that it is flattened
  2. Forgetting Sortkeys and getting a different key order each run
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.