Sometimes we need to peek into complex data structures, and trying to print them out can be tricky. That's where Data::Dumper comes to the rescue. For example, if we create this complex data structure:
$data = { array => [qw(apple banana clementine damson)], hash => { food => "vegetables", drink => "juice", }, };
how do we print it out? Very easily:
use Data::Dumper; print Dumper $data;
What we get is a pretty-printed $data:
$VAR1 = { 'hash' => { 'food' => 'vegetables', 'drink' => 'juice' }, 'array' => [ 'apple', 'banana', 'clementine', 'damson' ] };
Suppose while writing this example we made a mistake and wrote:
array => qw(apple banana clementine damson),
instead of:
array => [qw(apple banana clementine damson)],
When we pretty-printed the contents of $data we would immediately see our mistake:
$VAR1 = { 'banana' => 'clementine', 'damson' => 'hash', 'HASH(0x80cd79c)' => undef, 'array' => 'apple' };
That's not what we want—we have spotted the bug and can easily correct it.
You can use:
print STDERR Dumper $data;
or:
warn Dumper $data;
instead of printing to STDOUT, to have all the debug messages in the error_log file. This makes it even easier to debug your code, since the real output (which should normally go to the browser) is not mixed up with the debug output when the code is executed under mod_perl.
 
Continue to: