Data-Printer
view release on metacpan or search on metacpan
lib/Data/Printer.pm view on Meta::CPAN
tip instead.
=head2 Using Data::Printer from the Perl debugger
I<< (contributed by Ãrpád Szász and Marcel Grünauer (hanekomu)) >>
With L<DB::Pluggable>, you can easily set the perl debugger to use
Data::Printer to print variable information, replacing the debugger's
standard C<p()> function. All you have to do is add these lines to
your C<.perldb> file:
use DB::Pluggable;
DB::Pluggable->run_with_config( \'[DataPrinter]' ); # note the '\'
Then call the perl debugger as you normally would:
perl -d myapp.pl
Now Data::Printer's C<p()> command will be used instead of the debugger's!
See L<perldebug> for more information on how to use the perl debugger, and
L<DB::Pluggable> for extra functionality and other plugins.
If you can't or don't want to use DB::Pluggable, or simply want to keep
the debugger's C<p()> function and add an extended version using
Data::Printer (let's call it C<px()> for instance), you can add these
lines to your C<.perldb> file instead:
$DB::alias{px} = 's/px/DB::px/';
sub px {
my $expr = shift;
require Data::Printer;
print Data::Printer::p($expr);
}
Now, inside the Perl debugger, you can pass as reference to C<px> expressions
to be dumped using Data::Printer.
=head2 Using Data::Printer in a perl shell (REPL)
Some people really enjoy using a REPL shell to quickly try Perl code. One
of the most popular ones out there are L<Reply> and L<Devel::REPL>. If you
use them, now you can also see its output with Data::Printer!
=over 4
=item * B<Reply>
=back
Just install L<Reply::Plugin::DataPrinter> and add a line with
C<< [DataPrinter] >> to your C<.replyrc> file. That's it! Next time
you run the 'reply' REPL, Data::Printer will be used to dump variables!
=over 4
=item * B<Devel::REPL>
=back
Just install L<Devel::REPL::Plugin::DataPrinter> and add the following
line to your re.pl configuration file (usually ".re.pl/repl.rc" in your
home dir):
load_plugin('DataPrinter');
The next time you run C<re.pl>, it should dump all your REPL using
Data::Printer!
=head2 Easily rendering Data::Printer's output as HTML
To turn Data::Printer's output into HTML, you can do something like:
use HTML::FromANSI;
use Data::Printer;
my $html_output = ansi2html( np($object, colored => 1) );
In the example above, the C<$html_output> variable contains the
HTML escaped output of C<p($object)>, so you can print it for
later inspection or render it (if it's a web app).
=head2 Using Data::Printer with Template Toolkit
I<< (contributed by Stephen Thirlwall (sdt)) >>
If you use Template Toolkit and want to dump your variables using Data::Printer,
install the L<Template::Plugin::DataPrinter> module and load it in your template:
[% USE DataPrinter %]
The provided methods match those of C<Template::Plugin::Dumper>:
ansi-colored dump of the data structure in "myvar":
[% DataPrinter.dump( myvar ) %]
html-formatted, colored dump of the same data structure:
[% DataPrinter.dump_html( myvar ) %]
The module allows several customization options, even letting you load it as a
complete drop-in replacement for Template::Plugin::Dumper so you don't even have
to change your previous templates!
=head2 Migrating from Data::Dumper to Data::Printer
If you are porting your code to use Data::Printer instead of
Data::Dumper, you could replace:
use Data::Dumper;
with something like:
use Data::Printer;
sub Dumper { np @_, colored => 1 }
this sub will accept multiple variables just like Data::Dumper.
=head2 Unified interface for Data::Printer and other debug formatters
I<< (contributed by Kevin McGrath (catlgrep)) >>
( run in 1.602 second using v1.01-cache-2.11-cpan-71847e10f99 )