blib/lib/Data/Format/Pretty.pm

filter: Statementstmt 97.1% 34 / 35 Branchbran 75.0% 6 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 85.7% 6 / 7 Podpod 100% 2 / 2 totaltotal 89.4% 51 / 57 CC 6 SCAR 12.9
CC
6
Cov
91%
format_pretty
6.1
print_pretty
2.0
BEGIN
1.0
CRAP 3.6
↑ Summary
1 package Data::Format::Pretty;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Module::Loaded;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 require Exporter;
11 2 our @ISA = qw(Exporter);
12 2 our @EXPORT = qw(ppr);
13 2 our @EXPORT_OK = qw(format_pretty print_pretty ppr);
14  
15 2 our $VERSION = '0.04'; # VERSION
16  
17 3 ▶sub format_pretty {
Subroutine format_pretty: called CC=6 SCAR=18.0
Pod documented
18 3 my ($data, $opts0) = @_;
19  
20 3 ▶ my %opts = $opts0 ? %$opts0 : ();
Branch: 1 branch
BranchTrueFalseTotal
$opts0 ? : 3 0 3
21 3 my $module = $opts{module};
22 3 ▶ if (!$module) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($module) 2 1 3
23 2 ▶ if ($ENV{GATEWAY_INTERFACE} || $ENV{PLACK_ENV}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'GATEWAY_INTERFACE'} or $ENV{'PLACK_ENV'}) { } 1 1 2
Condition66%2/3
$ENV{"GATEWAY_INTERFACE"} or $ENV{"PLACK_ENV"}
l!l&&r!l&&!r
011
MC/DC50%1/2
$ENV{"GATEWAY_INTERFACE"} or $ENV{"PLACK_ENV"}
$ENV{"GATEWAY_INTERFACE"} $ENV{"PLACK_ENV"}
Truth table66%2/3
A or B
A= $ENV{"GATEWAY_INTERFACE"}B= $ENV{"PLACK_ENV"}
ABresult
1X1
011
000
24 1 $module = 'HTML';
25 } else {
26 1 $module = 'Console';
27 }
28 }
29 3 delete $opts{module};
30  
31 3 my $module_full = "Data::Format::Pretty::" . $module;
32 3 ▶ load $module_full unless is_loaded $module_full;
Branch: 1 branch
BranchTrueFalseTotal
unless &is_loaded($module_full) 3 0 3
33 3 my $sub = \&{$module_full . "::format_pretty"};
34  
35 3 $sub->($data, \%opts);
36 }
37  
38 0 ▶sub print_pretty {
Subroutine print_pretty: not called CC=1 SCAR=6.9
Pod documented
39 0 print format_pretty(@_);
40 }
41  
42 2 *ppr = \&print_pretty;
43  
44 2 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<Data::Format::Pretty::JSON>) for example. You only need to specify one
90 function, C<format_pretty>.
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<Data::Format::Pretty::Console>,
124 L<Data::Format::Pretty::HTML>, L<Data::Format::Pretty::JSON>,
125 L<Data::Format::Pretty::YAML>.
126  
127 Alternative data formatting framework/module family: L<Any::Renderer>.
128  
129 =head1 AUTHOR
130  
131 Steven Haryanto <stevenharyanto@gmail.com>
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__
↑ Summary