SYNOPSIS

Create your filter module:

  package Data::Printer::Filter::MyFilter;
  use strict;
  use warnings;

  use Data::Printer::Filter;

  # type filter
  filter 'SCALAR', sub {
      my ($ref, $properties) = @_;
      my $val = $$ref;

      if ($val > 100) {
          return 'too big!!';
      }
      else {
          return $val;
      }
  };

  # you can also filter objects of any class
  filter 'Some::Class', sub {
      my ($object, $properties) = @_;

      return $ref->some_method;   # or whatever

      # see 'HELPER FUNCTIONS' below for
      # customization options, including
      # proper indentation.
  };

  1;

Later, in your main code:

use Data::Printer { filters => { -external => [ 'MyFilter', 'OtherFilter' ],

# you can still add regular (inline) filters SCALAR => sub { ... } }, };

WARNING - ALPHA CODE (VERY LOOSE API)

We are still experimenting with the standalone filter syntax, so filters written like so may break in the future without any warning!

If you care, or have any suggestions, please drop me a line via \s-1RT\s0, email, or find me ('garu') on irc.perl.org.

You have been warned.

DESCRIPTION

Data::Printer lets you add custom filters to display data structures and objects, by either specifying them during \*(L"use\*(R", in the \*(C`.dataprinter\*(C' configuration file, or even in runtime customizations.

But there are times when you may want to group similar filters, or make them standalone in order to be easily reutilized in other environments and applications, or even upload them to \s-1CPAN\s0 so other people can benefit from a cleaner - and clearer - object/structure dump.

This is where \*(C`Data::Printer::Filter\*(C' comes in. It exports into your package's namespace the \*(L"filter\*(R" function, along with some helpers to create custom filter packages.

Data::Printer recognizes all filters in the \*(C`Data::Printer::Filter::*\*(C' namespace. You can load them by specifying them in the '-external' filter list (note the dash, to avoid clashing with a potential class or pragma labelled 'external'):

use Data::Printer { filters => { -external => 'MyFilter', }, };

This will load all filters defined by the \*(C`Data::Printer::Filter::MyFilter\*(C' module.

If there are more than one filter, use an array reference instead:

-external => [ 'MyFilter', 'MyOtherFilter' ]

\s-1IMPORTANT:\s0 \s-1THIS\s0 \s-1WAY\s0 \s-1OF\s0 \s-1LOADING\s0 \s-1EXTERNAL\s0 \s-1PLUGINS\s0 \s-1IS\s0 \s-1EXPERIMENTAL\s0 \s-1AND\s0 \s-1SUBJECT\s0 \s-1TO\s0 \s-1SUDDEN\s0 \s-1CHANGE\s0! \s-1IF\s0 \s-1YOU\s0 \s-1CARE\s0, \s-1AND/OR\s0 \s-1HAVE\s0 \s-1IDEAS\s0 \s-1ON\s0 A \s-1BETTER\s0 \s-1API\s0, \s-1PLEASE\s0 \s-1LET\s0 \s-1US\s0 \s-1KNOW\s0

HELPER FUNCTIONS

filter \s-1TYPE\s0, sub { ... };

The \*(C`filter\*(C' function creates a new filter for \s-1TYPE\s0, using the given subref. The subref receives two arguments: the item itself - be it an object or a reference to a standard Perl type - and the properties in effect (so you can inspect for certain options, etc). The subroutine is expected to return a string containing whatever it wants \*(C`Data::Printer\*(C' to display on screen.

p()

This is the same as \*(C`Data::Printer\*(C''s p(), only you can't rename it. You can use this to throw some data structures back at \*(C`Data::Printer\*(C' and use the results in your own return string - like when manipulating hashes or arrays.

\fInewline()\fP

This helper returns a string using the linebreak as specified by the caller's settings. For instance, it provides the proper indentation level of spaces for you and considers the \*(C`multiline\*(C' option to avoid line breakage.

In other words, if you do this:

filter ARRAY => { my ($ref, $p) = @_; my $string = "Hey!! I got this array:";

foreach my $val (@$ref) { $string .= newline . p($val); }

return $string; };

... your \*(C`p($val)\*(C' returns will be properly indented, vertically aligned to your level of the data structure, while simply using \*(L"\n\*(R" would just make things messy if your structure has more than one level of depth.

\fIindent()\fP

\fIoutdent()\fP

These two helpers let you increase/decrease the indentation level of your data display, for \*(C`newline()\*(C' and nested \*(C`p()\*(C' calls inside your filters.

For example, the filter defined in the \*(C`newline\*(C' explanation above would show the values on the same (vertically aligned) level as the \*(L"I got this array\*(R" message. If you wanted your array to be one level further deep, you could use this instead:

filter ARRAY => { my ($ref, $p) = @_; my $string = "Hey!! I got this array:";

indent; foreach my $val (@$ref) { $string .= newline . p($val); } outdent;

return $string; };

COLORIZATION

You can use Term::ANSIColor's \*(C`colored()\*(C'' for string colorization. Data::Printer will automatically enable/disable colors for you.

EXISTING FILTERS

This is meant to provide a complete list of standalone filters for Data::Printer available on \s-1CPAN\s0. If you write one, please put it under the \*(C`Data::Printer::Filter::*\*(C' namespace, and drop me a line so I can add it to this list!

Databases

Data::Printer::Filter::DB provides filters for Database objects. So far only \s-1DBI\s0 is covered, but more to come!

Dates & Times

Data::Printer::Filter::DateTime pretty-prints several date and time objects (not just DateTime) for you on the fly, including duration/delta objects!

Digest

Data::Printer::Filter::Digest displays a string containing the hash of the actual message digest instead of the object. Works on \*(C`Digest::MD5\*(C', \*(C`Digest::SHA\*(C', any digest class that inherits from \*(C`Digest::base\*(C' and some others that implement their own thing!

ClassicRegex

Data::Printer::Filter::ClassicRegex changes the way Data::Printer dumps regular expressions, doing it the classic \*(C`qr//\*(C' way that got popular in \*(C`Data::Dumper\*(C'.

\s-1URI\s0

Data::Printer::Filter::URI pretty-prints \s-1URI\s0 objects, displaying the \s-1URI\s0 as a string instead of dumping the object.

\s-1JSON\s0

Data::Printer::Filter::JSON lets you see your \s-1JSON\s0 structures replacing boolean objects with simple \*(C`true/false\*(C' strings!

URIs

Data::Printer::Filter::URI filters through several \s-1URI\s0 manipulation classes and displays the \s-1URI\s0 as a colored string. A very nice addition by Stanislaw Pusep (\s-1SYP\s0).

USING MORE THAN ONE FILTER FOR THE SAME TYPE/CLASS

As of version 0.13, standalone filters let you stack together filters for the same type or class. Filters of the same type are called in order, until one of them returns a string. This lets you have several filters inspecting the same given value until one of them decides to actually treat it somehow.

If your filter catched a value and you don't want to treat it, simply return and the next filter will be called. If there are no other filters for that particular class or type available, the standard Data::Printer calls will be used.

For example:

filter SCALAR => sub { my ($ref, $properties) = @_; if ( Scalar::Util::looks_like_number $$ref ) { return sprintf "%.8d", $$ref; } return; # lets the other SCALAR filter have a go };

filter SCALAR => sub { my ($ref, $properties) = @_; return qq["$$ref"]; };

Note that this \*(L"filter stack\*(R" is not possible on inline filters, since it's a hash and keys with the same name are overwritten. Instead, you can pass them as an array reference:

use Data::Printer filters => { SCALAR => [ sub { ... }, sub { ... } ], };

RELATED TO Data::Printer::Filter…

Data::Printer

LICENSE AND COPYRIGHT

Copyright 2011 Breno G. de Oliveira \*(C`<garu at cpan.org>\*(C'. 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.