File Coverage

blib/lib/Data/Format/Pretty.pm
Criterion Covered Total %
statement 28 29 96.5
branch 6 8 75.0
condition 2 3 66.6
subroutine 6 7 85.7
pod 2 2 100.0
total 44 49 89.8


line stmt bran cond sub pod time code
1             package Data::Format::Pretty;
2              
3 1     1   604 use 5.010001;
  1         3  
  1         39  
4 1     1   6 use strict;
  1         2  
  1         33  
5 1     1   5 use warnings;
  1         2  
  1         33  
6              
7 1     1   1692 use Module::Load;
  1         1058  
  1         5  
8 1     1   803 use Module::Loaded;
  1         564  
  1         304  
9              
10             require Exporter;
11             our @ISA = qw(Exporter);
12             our @EXPORT = qw(ppr);
13             our @EXPORT_OK = qw(format_pretty print_pretty ppr);
14              
15             our $VERSION = '0.04'; # VERSION
16              
17             sub format_pretty {
18 3     3 1 38649 my ($data, $opts0) = @_;
19              
20 3 50       20 my %opts = $opts0 ? %$opts0 : ();
21 3         9 my $module = $opts{module};
22 3 100       24 if (!$module) {
23 2 100 66     19 if ($ENV{GATEWAY_INTERFACE} || $ENV{PLACK_ENV}) {
24 1         3 $module = 'HTML';
25             } else {
26 1         2 $module = 'Console';
27             }
28             }
29 3         8 delete $opts{module};
30              
31 3         7 my $module_full = "Data::Format::Pretty::" . $module;
32 3 50       16 load $module_full unless is_loaded $module_full;
33 3         282505 my $sub = \&{$module_full . "::format_pretty"};
  3         20  
34              
35 3         16 $sub->($data, \%opts);
36             }
37              
38             sub print_pretty {
39 0     0 1   print format_pretty(@_);
40             }
41              
42             *ppr = \&print_pretty;
43              
44             1;
45             # ABSTRACT: Pretty-print data structure
46              
47              
48             =pod
49              
50             =head1 NAME
51              
52             Data::Format::Pretty - Pretty-print data structure
53              
54             =head1 VERSION
55              
56             version 0.04
57              
58             =head1 SYNOPSIS
59              
60             In your program:
61              
62             use Data::Format::Pretty qw(format_pretty print_pretty);
63              
64             # automatically choose an appropriate formatter
65             print format_pretty($data);
66              
67             # explicitly select a formatter
68             print format_pretty($data, {module=>'JSON'});
69              
70             # specify formatter option(s)
71             print format_pretty($data, {module=>'Console', interactive=>1});
72              
73             # shortcut for printing to output
74             print_pretty($data);
75              
76              
77             # ppr() is alias for print_pretty(), exported automatically. suitable for when
78             # debugging.
79             use Data::Format::Pretty;
80             ppr [1, 2, 3];
81              
82             =head1 DESCRIPTION
83              
84             Data::Format::Pretty is an extremely simple framework for pretty-printing data
85             structure. Its focus is on "prettiness" and automatic detection of appropriate
86             format to use.
87              
88             To develop a formatter, look at one of the formatter modules (like
89             L) for example. You only need to specify one
90             function, C.
91              
92             =head1 FUNCTIONS
93              
94             =head2 format_pretty($data, \%opts) => STR
95              
96             Send $data to formatter module (one of Data::Format::Pretty::* modules) and
97             return the result. Options:
98              
99             =over 4
100              
101             =item * module => STR
102              
103             Select the formatter module. It will be prefixed with "Data::Format::Pretty::".
104              
105             Currently if unspecified the default is 'Console', or 'HTML' if CGI/PSGI/plackup
106             environment is detected. In the future, more sophisticated detection logic will
107             be used.
108              
109             =back
110              
111             The rest of the options will be passed to the formatter module.
112              
113             =head2 print_pretty($data, \%opts)
114              
115             Just call format_pretty() and print() it.
116              
117             =head2 ppr($data, \%opts) [EXPORTED BY DEFAULT]
118              
119             Alias for print_pretty().
120              
121             =head1 SEE ALSO
122              
123             One of Data::Format::Pretty::* formatter, like L,
124             L, L,
125             L.
126              
127             Alternative data formatting framework/module family: L.
128              
129             =head1 AUTHOR
130              
131             Steven Haryanto
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is copyright (c) 2013 by Steven Haryanto.
136              
137             This is free software; you can redistribute it and/or modify it under
138             the same terms as the Perl 5 programming language system itself.
139              
140             =cut
141              
142              
143             __END__