The Dump-Value Command: C

The Dump-Value Command: C for the perl debugger.

To dump the entire expression thoroughly, use the x command, which will dump
the given value completely:

DB<13> x @x
0 ‘first’
1 ‘second’
2 ‘third’
3 ‘fourth’
4 ‘fifth’
DB<14>

That’s better, but it’s still a bit clunky, x is better used by giving
it a reference to the variable, this will then be dumped out with proper
indentation offering much more clarity:

DB<14> x \@x
0 ARRAY(0x83560d0)
0 ‘first’
1 ‘second’
2 ‘third’
3 ‘fourth’
4 ‘fifth’
DB<15>

Of course, the same thing can be done with multi-level data structures or
objects, when you can really get to see what an otherwise complex variable
consists of, which is where the indentation really becomes useful.

First we need to create our variable:

DB<15> %x = (\
cont: ‘this’ => ‘that’,\
cont: ‘chars’=> { ‘vowels’ => qw(a e i o u), ‘others’ => qw(s q w r k) },\
cont: ‘obj’ => bless({‘_member’ => ‘variable’, ‘_context’ => ‘init’}, ‘Class’),\
cont: )
DB<16>

We’ll include a self-referential reference too:

DB<16> $x{‘self ref’} = \%x
DB<17>

Now when we dump this out, everything becomes much more obvious. Note in
particular the reused memory reference indicated by the hexadecimal
number: 0x83ca658.

DB<17> x \%x
0 HASH(0x83a50f0)
‘chars’ => HASH(0x83ca658)
‘e’ => ‘i’
‘o’ => ‘u’
‘others’ => ‘s’
‘q’ => ‘w’
‘r’ => ‘k’
‘vowels’ => ‘a’
‘obj’ => Class=HASH(0x83ca838)
‘_context’ => ‘init’
‘_member’ => ‘variable’
‘self ref’ => HASH(0x83a50f0)
-> REUSED_ADDRESS
‘this’ => ‘that’
DB<18>

Although this variable dump looks a lot like that produced by Data::Dumper,
in fact this is produced by a core library called dumpvar.pl.

You can read more in the Pro-Perl-Debugging book.

Find this content useful? Share it with your friends or comment below!

Leave a Comment