doc updates
Breno G. de Oliveira
3 years ago
239 | 239 | |
240 | 240 | =head1 NAME |
241 | 241 | |
242 | Data::Printer - colored & full-featured pretty-print of Perl data structures and objects | |
242 | Data::Printer - colored & full-featured pretty print of Perl data structures and objects | |
243 | 243 | |
244 | 244 | =head1 SYNOPSIS |
245 | 245 | |
257 | 257 | # for anonymous array/hash references, use postderef (on perl 5.24 or later): |
258 | 258 | p [ $one, $two, $three ]->@*; |
259 | 259 | p { foo => $foo, bar => $bar }->%*; |
260 | ||
261 | # or deref the anonymous ref: | |
262 | p @{[ $one, $two, $three ]}; | |
263 | p %{{ foo => $foo, bar => $bar }}; | |
260 | 264 | |
261 | 265 | # or put '&' in front of the call: |
262 | 266 | &p( [ $one, $two, $three ] ); |
317 | 321 | |
318 | 322 | =item * B<< L<Filters|/Filters> for specific data structures and objects >> to make |
319 | 323 | debugging much, much easier. Includes filters for many popular classes |
320 | from CPAN like JSON::\*, URI, HTTP::\*, LWP, Digest::\*, DBI and DBIx::Class. | |
324 | from CPAN like JSON::*, URI, HTTP::*, LWP, Digest::*, DBI and DBIx::Class. | |
321 | 325 | printing what really matters to developers debugging code. It also lets you |
322 | 326 | create your own custom filters easily. |
323 | 327 | |
406 | 410 | p %some_hash; |
407 | 411 | p $scalar_or_ref; |
408 | 412 | |
409 | Note that anonymous structures will only work if you either use postderef | |
413 | Note that anonymous structures will only work if you postderef them: | |
414 | ||
415 | p [$foo, $bar, $baz]->@*; | |
416 | ||
417 | you may also deref it manually: | |
418 | ||
419 | p %{{ foo => $foo }}; | |
420 | ||
410 | 421 | or prefix C<p()> with C<&>: |
411 | 422 | |
412 | p [$foo, $bar, $baz]->@*; # postderef | |
413 | 423 | &p( [$foo, $bar, $baz] ); # & (note mandatory parenthesis) |
414 | 424 | |
415 | 425 | You can pass custom options that will work only on that particular call: |
417 | 427 | p @var, as => "some label", colorized => 0; |
418 | 428 | p %var, show_memsize => 1; |
419 | 429 | |
420 | By default C<p()> will print to STDERR and return the same variable being | |
430 | By default, C<p()> prints to STDERR and returns the same variable being | |
421 | 431 | dumped. This lets you quickly wrap variables with C<p()> without worrying |
422 | 432 | about changing return values. It means that if you change this: |
423 | 433 | |
456 | 466 | |
457 | 467 | There are 3 possible ways to customize Data::Printer: |
458 | 468 | |
459 | 1. Creating a C<.dataprinter> file on your home directory, on your | |
460 | project's base directory or wherever you set the C<DATAPRINTERRC> | |
461 | environment variable to. | |
469 | 1. B<[RECOMMENDED]> Creating a C<.dataprinter> file either on your home | |
470 | directory or your project's base directory, or both, or wherever you set | |
471 | the C<DATAPRINTERRC> environment variable to. | |
462 | 472 | |
463 | 473 | 2. Setting custom properties on module load. This will override any |
464 | setting from (1) on the namespace (package/module) it was called: | |
474 | setting from your config file on the namespace (package/module) it was called: | |
465 | 475 | |
466 | 476 | use DDP max_depth => 2, deparse => 1; |
467 | 477 | |
577 | 587 | class.internals = 1 |
578 | 588 | |
579 | 589 | |
580 | =head3 Settings shortcuts | |
590 | =head3 Settings' shortcuts | |
581 | 591 | |
582 | 592 | =over 4 |
583 | 593 | |
641 | 651 | and Moo(se) objects and is even aware of Role::Tiny. |
642 | 652 | |
643 | 653 | Data::Printer also comes with filter bundles that can be quickly activated |
644 | to make it easier to debug L<Data::Printer::Filter::ContentType|binary data> | |
654 | to make it easier to debug L<binary data|Data::Printer::Filter::ContentType> | |
645 | 655 | and many popular CPAN modules that handle |
646 | L<Data::Printer::Filter::DateTime|date and time>, | |
647 | L<Data::Printer::Filter::DB|databases> (yes, even DBIx::Class), | |
648 | L<Data::Printer::Filter::Digest|message digests> like MD5 and SHA1, and | |
649 | L<Data::Printer::Filter::Web|JSON and Web> content like HTTP requests and responses. | |
656 | L<date and time|Data::Printer::Filter::DateTime>, | |
657 | L<databases|Data::Printer::Filter::DB> (yes, even DBIx::Class), | |
658 | L<message digests|Data::Printer::Filter::Digest> like MD5 and SHA1, and | |
659 | L<JSON and Web|Data::Printer::Filter::Web> content like HTTP requests and | |
660 | responses. | |
650 | 661 | |
651 | 662 | So much so we recommend everyone to activate all bundled filters by putting |
652 | 663 | the following line on your C<.dataprinter> file: |
675 | 686 | |
676 | 687 | Notice that B<< you can do this without adding Data::Printer as a dependency >> |
677 | 688 | to your project! Just write your sub and it will be called with the object to |
678 | be printed and a C<$ddp> object ready for you. See L<Data::Printer::Object> for | |
679 | how to use it to pretty-print your data. | |
689 | be printed and a C<$ddp> object ready for you. See | |
690 | L<< Data::Printer::Object|/Data::Printer::Object/"Methods and Accessors for Filter Writers" >> | |
691 | for how to use it to pretty-print your data. | |
680 | 692 | |
681 | 693 | Finally, if your object implements string overload or provides a method called |
682 | 694 | "to_string", "as_string" or "stringify", Data::Printer will use it. To disable |
699 | 711 | p my @array = qw(a b c d); # wrong |
700 | 712 | my @array = qw(a b c d); p @array; # right |
701 | 713 | |
702 | On the default mode of C<< use_prototypes = 1 >>, you cannot pass anonymous | |
703 | data: | |
714 | If you pass a nonexistant key/index to DDP using prototypes, they | |
715 | will trigger autovivification: | |
716 | ||
717 | use DDP; | |
718 | my %foo; | |
719 | p $foo{bar}; # undef, but will create the 'bar' key (with undef) | |
720 | ||
721 | my @x; | |
722 | p $x[5]; # undef, but will initialize the array with 5 elements (all undef) | |
723 | ||
724 | Finally, as mentioned before, you cannot pass anonymous references on the | |
725 | default mode of C<< use_prototypes = 1 >>: | |
704 | 726 | |
705 | 727 | p { foo => 1 }; # wrong! |
706 | 728 | p %{{ foo => 1 }}; # right |
709 | 731 | sub pp { p @_ }; # wrapping it also lets you use anonymous data. |
710 | 732 | |
711 | 733 | use DDP use_prototypes => 0; |
712 | p { foo => 1 }; # works, but now you must always pass a reference to p() | |
713 | ||
714 | If you pass a nonexistant key/index to DDP using prototypes, they | |
715 | will trigger autovivification: | |
716 | ||
717 | use DDP; | |
718 | my %foo; | |
719 | p $foo{bar}; # undef, but will create the 'bar' key (with undef) | |
720 | ||
721 | my @x; | |
722 | p $x[5]; # undef, but will initialize the array with 5 elements (all undef) | |
734 | p { foo => 1 }; # works, but now p(@foo) will fail, you must always pass a ref, | |
735 | # e.g. p(\@foo) | |
723 | 736 | |
724 | 737 | =head1 BACKWARDS INCOMPATIBLE CHANGES |
725 | 738 | |
737 | 750 | =item * 1.00 - new C<.dataprinter> file format. |
738 | 751 | I<< This should only affect you if you have a C<.dataprinter> file. >> |
739 | 752 | The change was required to avoid calling C<eval> on potentially tainted/unknown |
740 | code. It also provided a much clearer interface. | |
753 | code. It also provided a much cleaner interface. | |
741 | 754 | |
742 | 755 | =item * 1.00 - new way of creating external filters. |
743 | 756 | I<< This only affects you if you write or use external filters. >> |
754 | 767 | C<< filters => [{ ... }] >> and you must replace C<< -external => [1,2] >> |
755 | 768 | with C<< filters => [1, 2] >>, or C<< filters => [1, 2, {...}] >> if you |
756 | 769 | also have inline filters. This allowed us much more power and flexibility |
757 | with filters, and hopefully also makes things cleaner. | |
770 | with filters, and hopefully also makes things clearer. | |
758 | 771 | |
759 | 772 | =item * 0.36 - C<p()>'s default return value changed from 'dump' to 'pass'. |
760 | 773 | This was a very important change to ensure chained calls and to prevent |