5.2. Viewing Complex Data with Data::DumperAnother way to visualize a complex data structure rapidly is to dump it. A particularly nice dumping package is included in the Perl core distribution, called Data::Dumper. Let's replace the last half of the byte-counting program with a simple call to Data::Dumper:
The Data::Dumper module defines the Dumper subroutine. This subroutine is similar to the x command in the debugger. You can give Dumper one or more values, and Dumper turns those values into a printable string. The difference between the debugger's x command and Dumper, however, is that the string generated by Dumper is Perl code:
The Perl code is fairly understandable; it shows that you have a reference to a hash of three elements, with each value of the hash being a reference to a nested hash. You can evaluate this code and get a hash that's equivalent to the original hash. However, if you're thinking about doing this in order to have a complex data structure persist from one program invocation to the next, please keep reading. Data::Dumper, like the debugger's x command, handles shared data properly. For example, go back to that "leaking" data from Chapter 4: use Data::Dumper; $Data::Dumper::Purity = 1; # declare possibly self-referencing structures my @data1 = qw(one won); my @data2 = qw(two too to); push @data2, \@data1; push @data1, \@data2; print Dumper(\@data1, \@data2); Here's the output from this program:
Notice how you've created two different variables now, since there are two parameters to Dumper. The element $VAR1 corresponds to a reference to @data1, while $VAR2 corresponds to a reference to @data2. The debugger shows the values similarly:
Note that the phrase REUSED_ADDRESS indicates that some parts of the data are actually references you've already seen.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|