blib/lib/HTML/HashTable.pm

filter: Statementstmt 90.2% 65 / 72 Branchbran 81.2% 13 / 16 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 9 totaltotal 81.4% 92 / 113 CC 13 SCAR 12.3
CC
13
Cov
88%
make_table
6.0
tablify
4.0
recurse_through
4.0
CRAP 3.4
↑ Summary
1  
2 # Items to export into callers namespace by default. Note: do not export
3 # names by default without a very good reason. Use EXPORT_OK instead.
4 # Do not simply export all your public functions/methods/constants.
5  
6 # This allows declaration use HTML::HashTable ':all';
7 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
8 # will save memory.
9  
10  
11 1 $VERSION = $VERSION = 0.60;
12  
13 1 require 5.005;
14  
15 package HTML::HashTable;
16  
17 1 require Exporter;
18 1 @ISA = qw( Exporter );
19 1 @EXPORT = qw(tablify);
20  
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 NAME
24  
25 C<HTML::HashTable> - Create an HTML table from a Perl hash
26  
27 =head1 SYNOPSIS
28  
29 use HTML::HashTable;
30 print tablify({
31 BORDER => 0,
32 DATA => $myhashref,
33 SORTBY => 'key',
34 ORDER => 'desc'}
35 );
36  
37 =head1 DESCRIPTION
38  
39 This module takes an arbitrary Perl hash and presents it as an HTML
40 table. The hash can contain anything you want -- scalar data,
41 arrayrefs, hashrefs, whatever. Yes, this means you can use a tied
42 hash if you wish.
43  
44 The HTML produced is nicely formatted and indented, suitable for
45 human editing and manipulation.
46  
47 Some options are provided with the tablify() function to allow you
48 to specify whether you wish to have a border or not, and whether you
49 wish your table to be sorted by key or value (but note that sorting
50 by value gives almost meaningless results if your values are
51 references, as in a deeply nested Perl data structure.)
52  
53 The options given to the tablify() function are:
54  
55 =item C<BORDER>
56  
57 True or false depending on whether you want your table to have a
58 border. Defaults to true (1).
59  
60 =item C<DATA>
61  
62 Reference to your hash
63  
64 =item C<SORTBY>
65  
66 Either 'key' or 'value' depending on how you want your data sorted.
67 Note that sorting by value is more or less meaningless if your
68 values are references (as in a deeply nested data structure). Defaults
69 to "key".
70  
71 =item C<ORDER>
72  
73 Either 'asc' or 'desc' depending on whether you want your sorting to
74 be in ascending or descending order. Defaults to "asc".
75  
76 =cut
77  
78 5 ▶sub tablify {
Subroutine tablify: called CC=4 SCAR=13.9
Pod undocumented
79 5 $HTML::HashTable::output = '';
80 5 $HTML::HashTable::depth = 0;
81 5 my $tsref = shift;
82 5 ▶ $tsref->{SORTBY} ||= "key";
Condition50%1/2
$tsref->{"SORTBY"} ||= "key"
l!l
05
MC/DC0%0/1
$tsref->{"SORTBY"} ||= "key"
$tsref->{"SORTBY"}
Truth table50%1/2
A
A= $tsref->{"SORTBY"}
Aresult
11
00
83 5 ▶ $tsref->{ORDER} ||= "asc";
Condition100%2/2
$tsref->{"ORDER"} ||= "asc"
l!l
14
MC/DC100%1/1
$tsref->{"ORDER"} ||= "asc"
$tsref->{"ORDER"}
Truth table100%2/2
A
A= $tsref->{"ORDER"}
Aresult
11
00
84 5 ▶ $tsref->{BORDER} = 1 unless (defined $tsref->{BORDER});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tsref->{"BORDER"} 3 2 5
85 5 make_table($tsref);
86 5 return $HTML::HashTable::output;
87 }
88  
89 #
90 # This subroutine does most of the work by recursing through the
91 # hash supplied. We look to see whether the value of any hash
92 # item is a scalar, an arrayref or a hashref, and act accordingly.
93 # Recursion's so rare in Perl... this is *fun*
94 #
95  
96 55 ▶sub recurse_through {
Subroutine recurse_through: called CC=4 SCAR=13.9
Pod undocumented
97 55 my $tsref = shift;
98 55 my $thingy = shift;
99 55 ▶ if (ref($thingy) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $thingy eq 'ARRAY') { } 10 45 55
elsif (ref $thingy eq 'HASH') { } 5 40 45
100 10 foreach (@$thingy) {
101 30 recurse_through($tsref, $_);
102 }
103 } elsif (ref($thingy) eq 'HASH') {
104 5 my $newref = {%$tsref};
105 5 $newref->{DATA} = $thingy;
106 5 open_cell();
107 5 make_table($newref);
108 5 close_cell($HTML::HashTable::depth);
109 } else { # plain old scalar data
110 40 open_cell();
111 40 $HTML::HashTable::output .= $thingy;
112 40 close_cell(0);
113 }
114 }
115  
116 10 ▶sub open_table {
Subroutine open_table: called CC=2 SCAR=6.9
Pod undocumented
117 10 my $tsref = shift;
118 10 $HTML::HashTable::output .= "\n";
119 10 $HTML::HashTable::output .= "\t" x $HTML::HashTable::depth;
120 10 ▶ $HTML::HashTable::output .= $tsref->{BORDER} ? "<table border=1>\n" : "<table border=0>\n";
Branch: 1 branch
BranchTrueFalseTotal
$tsref->{'BORDER'} ? : 8 2 10
121 }
122  
123 10 ▶sub close_table {
Subroutine close_table: called CC=1 SCAR=0.0
Pod undocumented
124 10 $HTML::HashTable::output .= "\t" x $HTML::HashTable::depth;
125 10 $HTML::HashTable::output .= "</table>\n";
126 }
127  
128 25 ▶sub open_row {
Subroutine open_row: called CC=1 SCAR=0.0
Pod undocumented
129 25 $HTML::HashTable::output .= "\t" x ($HTML::HashTable::depth);
130 25 $HTML::HashTable::output .= "<tr>\n";
131 25 $HTML::HashTable::depth++;
132 }
133  
134 25 ▶sub close_row {
Subroutine close_row: called CC=1 SCAR=0.0
Pod undocumented
135 25 $HTML::HashTable::depth--;
136 25 $HTML::HashTable::output .= "\t" x ($HTML::HashTable::depth);
137 25 $HTML::HashTable::output .= "</tr>\n";
138 }
139  
140 70 ▶sub open_cell {
Subroutine open_cell: called CC=1 SCAR=0.0
Pod undocumented
141 70 $HTML::HashTable::output .= "\t" x ($HTML::HashTable::depth);
142 70 $HTML::HashTable::output .= "<td>";
143 70 $HTML::HashTable::depth++;
144 }
145  
146 70 ▶sub close_cell {
Subroutine close_cell: called CC=2 SCAR=6.9
Pod undocumented
147 70 my $d = shift;
148 70 ▶ $d-- if $d;
Branch: 1 branch
BranchTrueFalseTotal
if $d 5 65 70
149 70 $HTML::HashTable::output .= "\t" x ($d);
150 70 $HTML::HashTable::output .= "</td>\n";
151 70 $HTML::HashTable::depth--;
152 }
153
154 10 ▶sub make_table {
Subroutine make_table: called CC=5 SCAR=18.0
Pod undocumented
155 10 my $tsref = shift;
156 10 open_table($tsref);
157 10 foreach my $key (sort {
158 20 ▶ if ($tsref->{SORTBY} eq "value") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tsref->{'SORTBY'} eq 'value') { } 0 20 20
159 0 ▶ if ($tsref->{ORDER} eq 'asc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tsref->{'ORDER'} eq 'asc') { } 0 0 0
160 0 ${$tsref->{DATA}}{$a} cmp ${$tsref->{DATA}}{$b};
161 } else {
162 0 ${$tsref->{DATA}}{$b} cmp ${$tsref->{DATA}}{$a};
163 }
164 } else {
165 20 ▶ if ($tsref->{ORDER} eq 'asc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tsref->{'ORDER'} eq 'asc') { } 16 4 20
166 16 $a cmp $b;
167 } else {
168 4 $b cmp $a;
169 }
170 }
171 10 } keys %{$tsref->{DATA}}) {
172 25 open_row;
173 25 open_cell;
174 25 $HTML::HashTable::output .= $key;
175 25 close_cell(0);
176 25 recurse_through($tsref, ${$tsref->{DATA}}{$key});
177 25 close_row;
178 }
179 10 close_table;
180 }
181  
182  
183  
184  
185 =head1 AUTHOR
186  
187 Kirrily "Skud" Robert <skud@cpan.org>
188  
189 =head1 SEE ALSO
190  
191 L<perl>.
192  
193 =cut
↑ Summary