File Coverage

blib/lib/Data/Dumper/Perltidy.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 26 26 100.0


line stmt bran cond sub pod time code
1             package Data::Dumper::Perltidy;
2              
3             ###############################################################################
4             #
5             # Data::Dumper::Perltidy - Dump and pretty print Perl data structures.
6             #
7             # Copyright 2009-2012, John McNamara.
8             #
9             # perltidy with standard settings.
10             #
11             # Documentation after __END__
12             #
13              
14 1     1   800 use strict;
  1         2  
  1         29  
15 1     1   5 use warnings;
  1         2  
  1         30  
16              
17 1     1   5 use Exporter;
  1         10  
  1         51  
18 1     1   3157 use Data::Dumper ();
  1         24990  
  1         33  
19 1     1   32867 use Perl::Tidy;
  1         413309  
  1         444  
20              
21             our $VERSION = '0.03';
22             our @EXPORT = ('Dumper');
23             our @ISA = qw(Exporter);
24             our $ARGV = '-npro -cab=1';
25              
26              
27             ###############################################################################
28             #
29             # Dumper()
30             #
31             # Overridden version of Data::Dumper::Dumper() with perltidy formatting.
32             #
33             sub Dumper {
34              
35 1     1 1 20 my $tidied;
36 1         9 my $dumper = Data::Dumper::Dumper(@_);
37              
38 1         172 perltidy( argv => $ARGV, source => \$dumper, destination => \$tidied );
39              
40 1         134340 return $tidied;
41             }
42              
43             1;
44              
45             __END__
46              
47             =pod
48              
49             =head1 NAME
50              
51             Data::Dumper::Perltidy - Dump and pretty print Perl data structures.
52              
53             =head1 SYNOPSIS
54              
55             To use C<Data::Dumper::Perltidy::Dumper()> to stringify and pretty print a Perl data structure:
56              
57             use Data::Dumper::Perltidy;
58              
59             ...
60              
61             print Dumper $some_data_structure;
62              
63             =head1 DESCRIPTION
64              
65             C<Data::Dumper::Perltidy> encapsulates both C<Data::Dumper> and C<Perl::Tidy> to provide a function that stringifies a Perl data structure in a pretty printed format. See the documentation for L<Data::Dumper> and L<Perl::Tidy> for further information.
66              
67             Data::Dumper can be used for, among other things, stringifying complex Perl data structures into a format that is suitable for printing and debugging.
68              
69             Perl::Tidy can be used to pretty print Perl code in a consistent and configurable manner.
70              
71             Data::Dumper also provides a certain level of pretty printing via the C<$Data::Dumper::Indent> variable but it isn't quite as nice as the Perl::Tidy output.
72              
73             Let's look at an example to see how this module can be used. Say you have a complex data structure that you wish to inspect. You can use the C<Data::Dumper::Perltidy::Dumper()> function as follows (note that the syntax is the same as Data::Dumper):
74              
75             #!/usr/bin/perl -w
76              
77             use strict;
78             use Data::Dumper::Perltidy;
79              
80             my $data = [{ title => 'This is a test header' },{ data_range =>
81             [ 0, 0, 3, 9 ] },{ format => 'bold' }];
82              
83             print Dumper $data;
84              
85             This would print out:
86              
87             $VAR1 = [
88             { 'title' => 'This is a test header' },
89             { 'data_range' => [ 0, 0, 3, 9 ] },
90             { 'format' => 'bold' }
91             ];
92              
93             By comparison the standard C<Data::Dumper::Dumper()> output would be:
94              
95             $VAR1 = [
96             {
97             'title' => 'This is a test header'
98             },
99             {
100             'data_range' => [
101             0,
102             0,
103             3,
104             9
105             ]
106             },
107             {
108             'format' => 'bold'
109             }
110             ];
111              
112             Which isn't too bad but if you are used to Perl::Tidy and the L<perltidy> utility you may prefer the C<Data::Dumper::Perltidy::Dumper()> output.
113              
114             =head1 FUNCTIONS
115              
116             =head2 Dumper()
117              
118             The C<Dumper()> function takes a list of perl structures and returns a stringified and pretty printed form of the values in the list. The values will be named C<$VARn> in the output, where C<n> is a numeric suffix.
119              
120             You can modify the Perl::Tidy output by passing arguments via the C<$Data::Dumper::Perltidy::ARGV> configuration variable:
121              
122             $Data::Dumper::Perltidy::ARGV = '-nst -mbl=2 -pt=0 -nola';
123              
124             See the L<Perl::Tidy> docs for more information on the available arguments. By default C<Data::Dumper::Perltidy> uses the argument C<-npro> to ignore any local C<.perltidyrc> configuration file.
125              
126             The Data::Dumper C<$Data::Dumper::> configuration variables can also be used to influence the output where applicable. For further information see the L<Data::Dumper> documentation.
127              
128             Note: unlike C<Data::Dumper::Dumper()> this function doesn't currently return a list of strings in a list context.
129              
130             =head1 RATIONALE
131              
132             I frequently found myself copying the output of C<Data::Dumper::Dumper()> into an editor so that I could run C<perltidy> on it. This module scratches that itch.
133              
134             =head1 LIMITATIONS
135              
136             This module doesn't attempt to implement all, or even most, of the functionality of C<Data::Dumper>.
137              
138             =head1 AUTHOR
139              
140             John McNamara C<< <jmcnamara@cpan.org> >>
141              
142             =head1 BUGS
143              
144             Please report any bugs or feature requests to L<https://github.com/jmcnamara/data-dumper-perltidy/issues> on Github.
145              
146             =head1 ACKNOWLEDGEMENTS
147              
148             The authors and maintainers of C<Data::Dumper> and C<Perl::Tidy>.
149              
150             =head1 SEE ALSO
151              
152             L<Data::Dump>
153              
154             L<Data::Printer>, which also has a full list of alternatives.
155              
156             =head1 COPYRIGHT & LICENSE
157              
158             Copyright 2009-2012 John McNamara, all rights reserved.
159              
160             This program is free software; you can redistribute it and/or modify it
161             under the same terms as Perl itself.
162              
163             =cut