11.11. Printing Data Structures11.11.2. SolutionIf the output's legibility and layout are important, write your own custom printing routine. If you are in the Perl debugger, use the x command:
From within your own programs, use the Dumper function from the standard module Data::Dumper: use Data::Dumper; print Dumper($reference); Or if you'd like output formatted in the same style as the Debugger uses: use Dumpvalue; Dumpvalue->new->dumpValue($reference); 11.11.3. DiscussionSometimes you'll want to make a dedicated function for your data structure that delivers a particular output format, but often this is overkill. If you're running under the Perl debugger, the x and X commands provide nice pretty-printing. The x command is more useful because it works on both global and lexical variables, whereas X works only on globals. Pass x a reference to the data structure you want to print.
The standard Dumpvalue module provides the Debugger's output formatting using an object-oriented interface. Here's an example:
which is like using the V main INC command in the Debugger. All the output formatting options from the Debugger are available from Dumpvalue. Just pass Dumpvalue->new option pairs: $dobj = Dumpvalue->new(option1 => value1, option2 => value2); Options available as of v5.8.1 include arrayDepth, hashDepth, compactDump, veryCompact, globPrint, dumpDBFiles, dumpPackages, dumpReused, tick, quoteHighBit, printUndef, usageOnly, unctrl, subdump, bareStringify, quoteHighBit, and stopDbSignal. The Data::Dumper module, also included in the standard Perl distribution, has a different approach. It provides a Dumper function that takes a list of references and returns a string with a printable (and eval able) form of those references. use Data::Dumper; print Dumper(\@INC); $VAR1 = [ '/usr/local/lib/perl5/5.8.1/OpenBSD.i386-openbsd', '/usr/local/lib/perl5/5.8.1', '/usr/local/lib/perl5/site_perl/5.8.1/OpenBSD.i386-openbsd', '/usr/local/lib/perl5/site_perl/5.8.1', '/usr/local/lib/perl5/site_perl/5.8.0/OpenBSD.i386-openbsd', '/usr/local/lib/perl5/site_perl/5.8.0', '/usr/local/lib/perl5/site_perl', '.' ]; Data::Dumper supports a variety of output formats. Check its documentation for details. Particularly useful is the option to decompile Perl code:
11.11.4. See AlsoThe documentation for Data::Dumper; Chapter 20 of Programming Perl or perldebug(1)
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|