File Coverage

blib/lib/Data/Printer/Filter/Digest.pm
Criterion Covered Total %
statement 19 19 100.0
branch 5 6 83.3
condition 3 6 50.0
subroutine 4 4 100.0
pod n/a
total 31 35 88.5


line stmt bran cond sub pod time code
1             package Data::Printer::Filter::Digest;
2 1     1   5 use strict;
  1         1  
  1         25  
3 1     1   37 use warnings;
  1         2  
  1         36  
4 1     1   3 use Data::Printer::Filter;
  1         2  
  1         6  
5              
6             filter 'Digest::base' => \&_print_digest;
7              
8             # these modules don't inherit from Digest::base but have the same interface:
9             filter 'Digest::MD2' => \&_print_digest;
10             filter 'Digest::MD4' => \&_print_digest;
11              
12             sub _print_digest {
13 9     9   18 my ($obj, $ddp) = @_;
14 9         70 my $digest = $obj->clone->hexdigest;
15 9         29 my $str = $digest;
16 9         13 my $ref = ref $obj;
17              
18 9 100 66     17 if ( !exists $ddp->extra_config->{filter_digest}{show_class_name}
19             || $ddp->extra_config->{filter_digest}{show_class_name} ) {
20 6         12 $str .= " ($ref)";
21             }
22              
23 9 50 33     75 if( !exists $ddp->extra_config->{filter_digest}{show_reset}
24             || $ddp->extra_config->{filter_digest}{show_reset}
25             ) {
26 9 100       51 if ($digest eq $ref->new->hexdigest) {
27 3         15 $str .= ' [reset]';
28             }
29             }
30              
31 9         50 return $ddp->maybe_colorize($str, 'datetime', '#ffaaff');
32             }
33              
34             1;
35              
36             __END__
37              
38             =head1 NAME
39              
40             Data::Printer::Filter::Digest - pretty-printing MD5, SHA and many other digests
41              
42             =head1 SYNOPSIS
43              
44             In your C<.dataprinter> file:
45              
46             filters = Digest
47              
48             You may also setup the look and feel with the following options:
49              
50             filter_digest.show_class_name = 0
51             filter_digest.show_reset = 1
52              
53             # you can even customize your themes:
54             colors.digest = #27ac3c
55              
56             That's it!
57              
58             =head1 DESCRIPTION
59              
60             This is a filter plugin for L<Data::Printer>. It filters through
61             several message digest objects and displays their current value in
62             hexadecimal format as a string.
63              
64             =head2 Parsed Modules
65              
66             Any module that inherits from L<Digest::base>. The following ones
67             are actively supported:
68              
69             =over 4
70              
71             =item * L<Digest::Adler32>
72              
73             =item * L<Digest::MD2>
74              
75             =item * L<Digest::MD4>
76              
77             =item * L<Digest::MD5>
78              
79             =item * L<Digest::SHA>
80              
81             =item * L<Digest::SHA1>
82              
83             =item * L<Digest::Whirlpool>
84              
85             =back
86              
87             If you have any suggestions for more modules or better output,
88             please let us know.
89              
90             =head2 Extra Options
91              
92             Aside from the display color, there are a few other options to
93             be customized via the C<filter_digest> option key:
94              
95             =head3 show_class_name
96              
97             If set to true (the default) the class name will be displayed
98             right next to the hexadecimal digest.
99              
100             =head3 show_reset
101              
102             If set to true (the default), the filter will add a C<[reset]>
103             tag after dumping an empty digest object. See the rationale below.
104              
105             =head2 Note on dumping Digest::* objects
106              
107             The digest operation is effectively a destructive, read-once operation. Once
108             it has been performed, most Digest::* objects are automatically reset and can
109             be used to calculate another digest value.
110              
111             This behaviour - or, rather, forgetting about this behaviour - is
112             a common source of issues when working with Digests.
113              
114             This Data::Printer filter will B<not> destroy your object. Instead, we
115             work on a I<cloned> version to display the hexdigest, leaving your
116             original object untouched.
117              
118             As another debugging convenience for developers, since the empty
119             object will produce a digest even after being used, this filter
120             adds by default a C<[reset]> tag to indicate that the object is
121             empty, in a 'reset' state - i.e. its hexdigest is the same as
122             the hexdigest of a new, empty object of that same class.
123              
124             =head1 SEE ALSO
125              
126             L<Data::Printer>