11.11. Printing Data StructuresProblemYou want to print out a data structure. SolutionIf the output's legibility and layout are important, write your own custom printing routine.
If you are in the Perl debugger, use the DB<1> $reference = [ { "foo" => "bar" }, 3, sub { print "hello, world\n" } ]; DB<2> x $reference
From within your own programs, use the use Data::Dumper; print Dumper($reference); Discussion
Sometimes 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 D<1> x \@INC These commands use the dumpvar.pl library. Here's an example: { package main; require "dumpvar.pl" } *dumpvar = \&main::dumpvar if __PACKAGE__ ne 'main'; dumpvar("main", "INC"); # show both @INC and %INC
The
dumpvar.pl
library isn't a module, but we wish it were - so we cajole it into exporting its
The Data::Dumper module, located on CPAN, has a more flexible solution. It provides a use Data::Dumper; print Dumper(\@INC); Data::Dumper supports a variety of output formats. Check its documentation for details. See AlsoThe documentation for the CPAN module Data::Dumper; the section "The Perl Debugger" from Chapter 8 of Programming Perl or perldebug (1) Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|