++ed by:
TOKUHIROM TOBYINK MIROD SHARYANTO BOWTIE

54 PAUSE users
36 non-PAUSE users.

Breno G. de Oliveira

NAME

Data::Printer - colored pretty-print of Perl data structures and objects

SYNOPSIS

  use Data::Printer;

  my @array = qw(a b);
  $array[3] = 'c';
  
  p @array;  # no need to pass references!

Code above will show this (with colored output):

   [
       [0] "a",
       [1] "b",
       [2] undef,
       [3] "c",
   ]

You can also inspect Objects:

    my $obj = SomeClass->new;

    p($obj);

Which might give you something like:

  \ SomeClass  {
      Parents       Moose::Object
      Linear @ISA   SomeClass, Moose::Object
      public methods (3) : bar, foo, meta
      private methods (0)
      internals: {
         _something => 42,
      }
  }
 

If for some reason you want to mangle with the output string instead of printing it to STDERR, you can simply ask for a return value:

  my $string = p(@some_array);
  warn p(%some_hash);

Finally, you can set all options during initialization, including coloring, identation and filters!

  use Data::Printer {
      color => {
         'regex' => 'blue',
         'hash'  => 'yellow',
      },
      filters => {
         'DateTime' => sub { $_[0]->ymd },
         'SCALAR'   => sub { "oh noes, I found a scalar! $_[0]" },
      },
  };

And if you like your setup better than the defaults, just put them in a '.dataprinter' file in your home dir and don't repeat yourself ever again :)

RATIONALE

Data::Dumper is a fantastic tool, meant to stringify data structures in a way they are suitable for being eval'ed back in.

The thing is, a lot of people keep using it (and similar ones, like Data::Dump) to print data structures and objects on screen for inspection and debugging, and while you can use those modules for that, it doesn't mean mean you should.

This is where Data::Printer comes in. It is meant to do one thing and one thing only:

display Perl variables and objects on screen, properly formatted (to be inspected by a human)

If you want to serialize/store/restore Perl data structures, this module will NOT help you. Try Storable, Data::Dumper, JSON, or whatever. CPAN is full of such solutions!

COLORS

Below are all the available colorizations and their default values. Note that both spellings ('color' and 'colour') will work.

   use Data::Printer {
     color => {
        array    => 'bright_white',  # array index numbers
        number   => 'bright_blue',   # numbers
        string   => 'bright_yellow', # strings
        class    => 'bright_green',  # class names
        undef    => 'bright_red',    # the 'undef' value
        hash     => 'magenta',       # hash keys
        regex    => 'yellow',        # regular expressions
        code     => 'green',         # code references
        glob     => 'bright_cyan',   # globs (usually file handles)
        repeated => 'white on_red',  # references to seen values
     },
   };

FILTERS

Data::Printer offers you the ability to use filters to override any kind of data display. The filters are placed on a hash, where the keys are the types or class names, and the values are anonymous subs that receive the item itself as a parameter. This lets you quickly override the way Data::Printer handles and displays data types and, in particular, objects.

  use Data::Printer {
        filters => {
            'DateTime'      => sub { $_[0]->ymd },
            'HTTP::Request' => sub { $_[0]->uri },
        },
  };

Perl types are named as ref calls them: SCALAR, ARRAY, HASH, REF, CODE, Regexp and GLOB. As for objects, just use the object's name, as shown above.

Note: If you plan on calling p() from within a filter, please make sure you are passing only REFERENCES as arguments. See "CAVEATS" below.

ALIASING

Data::Printer provides the nice, short, p() function to dump your data structures and objects. In case you rather use a more explicit name, already have a p() function (why?) in your code and want to avoid clashing, or are just used to other function names for that purpose, you can easily rename it:

  use Data::Printer { alias => 'Dumper' };

  Dumper( %foo );

CONFIGURATION FILE (RUN CONTROL)

Data::Printer tries to let you easily customize as much as possible regarding the visualization of your data structures and objects. But we don't want you to keep repeating yourself every time you want to use it!

To avoid this, you can simply create a file called .dataprinter in your home directory (usually /home/username in Linux), and put your configuration hash reference in there.

This way, instead of doing something like:

   use Data::Printer {
     colour => {
        array => 'bright_blue',
     },
     filters => {
         'Catalyst::Request' => sub {
             my $req = shift;
             return "Cookies: " . p($req->cookies)
         },
     },
   };

You can create a .dataprinter file like this:

   {
     colour => {
        array => 'bright_blue',
     },
     filters => {
         'Catalyst::Request' => sub {
             my $req = shift;
             return "Cookies: " . p($req->cookies)
         },
     },
   };

and from then on all you have to do while debugging scripts is:

  use Data::Printer;

and it will load your custom settings every time :)

EXPERIMENTAL FEATURES

The following are volatile parts of the API which are subject to change at any given version. Use them at your own risk.

Local Configuration (experimental!)

You can override global configurations by writing them as the second parameter for p(). For example:

  p( %var, color => { hash => 'green' } );

CAVEATS

You can't pass more than one variable at a time.

   p($foo, $bar); # wrong
   p($foo);       # right
   p($bar);       # right

You are supposed to pass variables, not anonymous structures:

   p( { foo => 'bar' } ); # wrong

   p %somehash;        # right
   p $hash_ref;        # also right

If you are using filters, and calling p() (or whatever name you aliased it to) from inside those filters, you must pass the arguments to p() as a reference:

  use Data::Printer {
      filters => {
          ARRAY => sub {
              my $listref = shift;
              my $string = '';
              foreach my $item (@$listref) {
                  $string .= p( \$item );      # p( $item ) will not work!
              }
              return $string;
          },
      },
  };

This happens because your filter is compiled before Data::Printer itself loads, so the filter does not see the function prototype. If you forget to pass a reference, Data::Printer will generate an exception for you with the following message:

    'If you call p() inside a filter, please pass arguments as references'

BUGS

If you find any, please file a bug report.

SEE ALSO

Data::Dumper

Data::Dump

Data::Dumper::Concise

Data::Dump::Streamer

Data::TreeDumper

LICENSE AND COPYRIGHT

Copyright 2011 Breno G. de Oliveira <garu at cpan.org>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.