Perl Cookbook

Perl CookbookSearch this book
Previous: 11.10. Reading and Writing Hash Records to Text FilesChapter 11
References and Records
Next: 11.12. Copying Data Structures
 

11.11. Printing Data Structures

Problem

You want to print out a data structure.

Solution

If 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:

DB<1> $reference = [ { "foo" => "bar" }, 3, sub { print "hello, world\n" } ];
DB<2> x $reference
  0  ARRAY(0x1d033c)
    0  HASH(0x7b390)
       'foo' = 'bar'>
    1  3
    2  CODE(0x21e3e4)
       - & in ???>

From within your own programs, use the Dumper function from the CPAN module Data::Dumper:

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 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 only works on globals. Pass x a reference to the data structure you want to print.

D<1> x \@INC
  0  ARRAY(0x807d0a8)
     0  '/home/tchrist/perllib' 
     1  '/usr/lib/perl5/i686-linux/5.00403'
     2  '/usr/lib/perl5' 
     3  '/usr/lib/perl5/site_perl/i686-linux' 
     4  '/usr/lib/perl5/site_perl' 
     5  '.'

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 dumpvar function anyway. The first two lines forcibly import the main::dumpvar function from package main into the current package, assuming it's different. Here's the output of that call:

@INC = (
   0  '/home/tchrist/perllib/i686-linux'
   1  '/home/tchrist/perllib'
   2  '/usr/lib/perl5/i686-linux/5.00404'
   3  '/usr/lib/perl5'
   4  '/usr/lib/perl5/site_perl/i686-linux'
   5  '/usr/lib/perl5/site_perl'
   6  '.'
)
%INC = (
   'dumpvar.pl' = '/usr/lib/perl5/i686-linux/5.00404/dumpvar.pl'
   'strict.pm' = '/usr/lib/perl5/i686-linux/5.00404/strict.pm'
)

The Data::Dumper module, located on CPAN, has a more flexible solution. It provides a Dumper function that takes a list of references and returns a string with a printable (and evalable) form of those references.

use Data::Dumper; 
print Dumper(\@INC); 
$VAR1 = [
      '/home/tchrist/perllib', 
      '/usr/lib/perl5/i686-linux/5.00403',
      '/usr/lib/perl5', 
      '/usr/lib/perl5/site_perl/i686-linux',
      '/usr/lib/perl5/site_perl', 
      '.'
];

Data::Dumper supports a variety of output formats. Check its documentation for details.

See Also

The documentation for the CPAN module Data::Dumper; the section "The Perl Debugger" from Chapter 8 of Programming Perl or perldebug (1)


Previous: 11.10. Reading and Writing Hash Records to Text FilesPerl CookbookNext: 11.12. Copying Data Structures
11.10. Reading and Writing Hash Records to Text FilesBook Index11.12. Copying Data Structures

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.