blib/lib/HTML/Lister.pm

filter: Statementstmt 7.4% 4 / 54 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 4 Podpod 100% 4 / 4 totaltotal 3.9% 8 / 202 CC 47 SCAR 55.9
CC
47
Cov
2%
hashList
342.0
List
240.0
oList
240.0
CRAP 267.4
↑ Summary Lister/test.pl »
1 package HTML::Lister;
2  
3 1 $VERSION = '0.01';
4  
5 1 my $aryparams = '';
6 1 my $hashparams = '';
7  
8 0 ▶sub List {
Subroutine List: not called CC=15 SCAR=54.8
Pod documented
9 0 my $class = shift;
10 0 $class->{'caller'} = 'List';
11 0 my @list;
12 0 ▶ push @list, sprintf "<ul%s>",
Branch: 1 branch
BranchTrueFalseTotal
$aryparams ? : 0 0 0
13 $aryparams ?
14 " $aryparams":
15 undef;
16 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
17 0 my $val = shift;
18 0 my $ref = ref $val;
19 0 ▶ unless ($ref eq 'SCALAR' or
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE" or not $ref) 0 0 0
Condition0%0/21
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE" or not $ref
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/8
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE" or not $ref
$ref eq "SCALAR" $ref eq "ARRAY" $ref eq "HASH" $ref eq "CODE" $ref eq "REF" $ref eq "GLOB" $ref eq "LVALUE" not $ref
Truth table0%0/9
A or B or C or D or E or F or G or not(H)
A= $ref eq "SCALAR"B= $ref eq "ARRAY"C= $ref eq "HASH"D= $ref eq "CODE"E= $ref eq "REF"F= $ref eq "GLOB"G= $ref eq "LVALUE"H= not $ref
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
20 $ref eq 'ARRAY' or
21 $ref eq 'HASH' or
22 $ref eq 'CODE' or
23 $ref eq 'REF' or
24 $ref eq 'GLOB' or
25 $ref eq 'LVALUE' or
26 !$ref) {
27 0 my $refval = sprintf '%s', $val;
28 0 my ($class,$type) = $refval =~ /^(.*)\=(.*)\(.*\)$/;
29 0 $ref = $type;}
30 0 ▶ push @list, sprintf "%s",
Branch: 4 branches
BranchTrueFalseTotal
$ref ? : 0 0 0
$ref eq 'SCALAR' ? : 0 0 0
$ref eq 'ARRAY' ? : 0 0 0
$ref eq 'HASH' ? : 0 0 0
31 $ref ?
32 $ref eq 'SCALAR' ?
33 sprintf "<li>%s</li>", $$val :
34 $ref eq 'ARRAY' ?
35 $class->List(@$val) :
36 $ref eq 'HASH' ?
37 $class->hashList(%$val) :
38 sprintf "<li><b>%s</b></li>(%s)",
39 $ref, $val :
40 "<li>$val</li>";}
41 0 push @list, '</ul>';
42 0 return join "\n", @list;}
43  
44 0 ▶sub oList {
Subroutine oList: not called CC=15 SCAR=54.8
Pod documented
45 0 my $class = shift;
46 0 $class->{'caller'} = 'oList';
47 0 my @list;
48 0 ▶ push @list, sprintf "<ol%s>",
Branch: 1 branch
BranchTrueFalseTotal
$aryparams ? : 0 0 0
49 $aryparams ?
50 " $aryparams":
51 undef;
52 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
53 0 my $val = shift;
54 0 my $ref = ref $val;
55 0 ▶ unless ($ref eq 'SCALAR' or
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE" or not $ref) 0 0 0
Condition0%0/21
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE" or not $ref
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/8
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE" or not $ref
$ref eq "SCALAR" $ref eq "ARRAY" $ref eq "HASH" $ref eq "CODE" $ref eq "REF" $ref eq "GLOB" $ref eq "LVALUE" not $ref
Truth table0%0/9
A or B or C or D or E or F or G or not(H)
A= $ref eq "SCALAR"B= $ref eq "ARRAY"C= $ref eq "HASH"D= $ref eq "CODE"E= $ref eq "REF"F= $ref eq "GLOB"G= $ref eq "LVALUE"H= not $ref
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
56 $ref eq 'ARRAY' or
57 $ref eq 'HASH' or
58 $ref eq 'CODE' or
59 $ref eq 'REF' or
60 $ref eq 'GLOB' or
61 $ref eq 'LVALUE' or
62 !$ref) {
63 0 my $refval = sprintf '%s', $val;
64 0 my ($class,$type) = $refval =~ /^(.*)\=(.*)\(.*\)$/;
65 0 $ref = $type;}
66 0 ▶ push @list, sprintf "%s",
Branch: 4 branches
BranchTrueFalseTotal
$ref ? : 0 0 0
$ref eq 'SCALAR' ? : 0 0 0
$ref eq 'ARRAY' ? : 0 0 0
$ref eq 'HASH' ? : 0 0 0
67 $ref ?
68 $ref eq 'SCALAR' ?
69 sprintf "<li>%s</li>", $$val :
70 $ref eq 'ARRAY' ?
71 $class->List(@$val) :
72 $ref eq 'HASH' ?
73 $class->hashList(%$val) :
74 sprintf "<li><b>%s</b></li>(%s)",
75 $ref, $val :
76 "<li>$val</li>";}
77 0 push @list, '</ol>';
78 0 return join "\n", @list;}
79  
80 0 ▶sub hashList {
Subroutine hashList: not called CC=18 SCAR=58.3
Pod documented
81 0 my $class = shift;
82 0 my $caller = $class->{'caller'};
83 0 my @list;
84 my @stuff;
85 0 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
86 0 ▶ while (my ($k,$v) = each %{$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$_[0];}) 0 0 0
87 0 push @stuff, $k, $v;}}
88 else {
89 0 @stuff = @_;}
90 0 ▶ push @list, sprintf "<dl>%s",
Branch: 1 branch
BranchTrueFalseTotal
$hashparams ? : 0 0 0
91 $hashparams ?
92 " $hashparams":
93 undef;
94 0 ▶ while (@stuff) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stuff) 0 0 0
95 0 push @list, sprintf "<dt>%s</dt>", shift(@stuff);
96 0 my $val = shift @stuff;
97 0 my $ref = ref $val;
98 0 ▶ unless ($ref eq 'SCALAR' or
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE" or not $ref) 0 0 0
Condition0%0/21
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE" or not $ref
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH"
l!l&&r!l&&!r
000
$ref eq "SCALAR" or $ref eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/8
$ref eq "SCALAR" or $ref eq "ARRAY" or $ref eq "HASH" or $ref eq "CODE" or $ref eq "REF" or $ref eq "GLOB" or $ref eq "LVALUE" or not $ref
$ref eq "SCALAR" $ref eq "ARRAY" $ref eq "HASH" $ref eq "CODE" $ref eq "REF" $ref eq "GLOB" $ref eq "LVALUE" not $ref
Truth table0%0/9
A or B or C or D or E or F or G or not(H)
A= $ref eq "SCALAR"B= $ref eq "ARRAY"C= $ref eq "HASH"D= $ref eq "CODE"E= $ref eq "REF"F= $ref eq "GLOB"G= $ref eq "LVALUE"H= not $ref
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
99 $ref eq 'ARRAY' or
100 $ref eq 'HASH' or
101 $ref eq 'CODE' or
102 $ref eq 'REF' or
103 $ref eq 'GLOB' or
104 $ref eq 'LVALUE' or
105 !$ref) {
106 0 my $refval = sprintf '%s', $val;
107 0 my ($class,$type) = $refval =~ /^(.*)\=(.*)\(.*\)$/;
108 0 $ref = $type;}
109 0 ▶ push @list, sprintf "<dd>%s</dd>",
Branch: 5 branches
BranchTrueFalseTotal
$ref ? : 0 0 0
$ref eq 'SCALAR' ? : 0 0 0
$ref eq 'ARRAY' ? : 0 0 0
$caller eq 'oList' ? : 0 0 0
$ref eq 'HASH' ? : 0 0 0
110 $ref ?
111 $ref eq 'SCALAR' ?
112 $$val :
113 $ref eq 'ARRAY' ?
114 $caller eq 'oList' ?
115 $class->oList(@$val) :
116 $class->List(@$val) :
117 $ref eq 'HASH' ?
118 $class->hashList(%$val) :
119 sprintf "<b>%s</b>(%s)",
120 $ref, $val :
121 $val;}
122 0 push @list, '</dl>';
123 0 return join "\n", @list;}
124  
125 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
126 0 my $class = shift;
127 0 ▶ return bless {}, ref $class || $class; }
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
128  
129 1 1||'Happy Happy, Joy Joy'; # 1s are so boring
130  
131 =head1 NAME
132  
133 HTML::Lister - Multidimensional Structure to HTML List Converter
134  
135 =head1 SYNOPSIS
136  
137 use HTML::Lister;
138 my $lister = new HTML::Lister;
139 print "Content-type: text/html\n\n";
140 print $lister->List($arrayref);
141 print $lister->oList(@array);
142 print $lister->hashList(%hash);
143 print $lister->List($hashref);
144  
145 =head1 DESCRIPTION
146  
147 HTML::Lister is a small HTML utility that addresses a built-in function that CGI.pm has left
148 unaddressed -- it handles multidimensional objects, calling itself recursively to create an
149 HTML list entity from the contents.
150  
151 Honestly, I hacked this thing together quickly out of need, but it's becomes somehting I use
152 on a regular basis, debugging CGI scripts live through a browser and so on.
153  
154 Put then again, that's where Perl itself came from!
155  
156 =head1 METHODS
157  
158 =over 4
159  
160 =item new() (constructor method)
161  
162 This is just the typical constructor method. It currently takes no arguments and provides no options.
163  
164 =item List()
165  
166 List() is the primary method of Lister. It takes any array, arrayref, hashref, or scalar value
167 (though the latter is admittedly pointless) and makes an unordered list (UL entity) out of what
168 it gets.
169  
170 If it receives a hashref, it calls hashList on the dereferences hashref, making an unordered
171 list with no LI entities, but with a definition list inside it. This has the potentially annoying
172 side effect of indenting any lone hashref it's handed, but it will still work and doesn't matter for
173 debugging purposes, which is Lister's main utilisation. If you want to avoid this, hand it off to
174 hashList() instead.
175  
176 An array or arrayref handed to List will create a proper bulleted unordered list. A scalar or
177 scalarref handed off to List() will return a list of one item.
178  
179 If List() encounters a reference in any of its argument list, it will dereference it and call the
180 appropriate method on that item if it dereferences to an array or hash. If it's a hashref, it will
181 call hashList. If it's an array, it will call itself. That's also why just handing it a hashref
182 works, but handing it a real hash won't -- the keys and values end up as @_ as an array.
183  
184 Oh, yeah -- a plain list or listref works nicely, too. (It's an array -- @_ -- by the time the
185 method sees it anyway, which is also why handing it a hash won't work).
186  
187 =item oList()
188  
189 The oList() method works just like the List() method, except that the lists it creates are
190 HTML OL entities -- that is, ordered lists. That means they're numbered (or lettered, or whatever,
191 depending on their nestedness).
192  
193 It should be noted somewhere (and here seems like a good place) that if you call oList, and oList
194 finds a hashref, and that hash contains an arrayref, the object will in fact remember where it
195 came from and call oList again, not List. In other words, if you start with numbered lists, you
196 will get numbered lists all the way in. If you start with bulleted lists, you will get bulleted
197 lists all the way in. This is to be considered cool.
198  
199 =item hashList()
200  
201 The hashList() method takes a hash or hashref as an argument. You can use it in a utility fashion,
202 if you want, and hand it an array or list, too, and it may give you what you expect. Handing it
203 an arrayref or listref almost definitely will *not* do what you want. hashList creates an HTML
204 definition list (DL entity). The keys become DT entities and the values become DD entities. In the
205 case of handing it an array or list, the even-indexed items become DTs and the odd-indexed items
206 become DDs.
207  
208 As with the other two methods, hashList() recognises when it encounters a reference, dereferences it,
209 and calls the appropriate method on it. If it is called explicitly, sub-lists will be UL entities
210 (bulleted lists).
211  
212 Do not hand hashList a blessed hashref... it won't work. Dereference it first (i.e. hashList(%$cgi)
213 or hand it off through List, which will do the job for you. Honestly, I'm still scratching my head
214 on this one. I'm sure there's a perfectly rational explanation, and I think I have come up with it
215 on a number of occassions, but I never remember when the Sudafed and NyQuil wear off.
216  
217 =item notes on all methods
218  
219 None of these methods are intended for use with other kinds of references. They won't break it,
220 but you will simply get GLOB(0x80f1098) or CODE(0x80fd9d8) or whatever. It can't see past that
221 point. Nevertheless, this is a far cry better than the listing functions built into CGI.pm right
222 now (no offense to that wonderful module that I use all the time or it's writers or maintainers --
223 you can't do everything).
224  
225 =item CSS support and additional tag parametres
226  
227 I'll admit this is badly kludged in. You can access the variables $HTML::Lister::aryparams and
228 $HTML::Lister::hashparams directly to set up additional paramatres for the HTML code created.
229 Whatever is in these variables will appear in the entity's tag, right after the *l -- thus you
230 will want to start anything you put here with a space.
231  
232 For instance, to set a CSS class on your definition lists, you might do this:
233 $HTML::Lister::hashparams = ' class="purdy"'
234 and your HTML entity will look like this:
235 <dl class="purdy">
236  
237 These default to '', so the list entities default to being a plain tag ('<ul>,<dl>,<ol>')
238  
239 =back
240  
241 =head1 TO DO LIST
242  
243 It would be nice if I could get some help building a non-OO version of this, too. However, I'm not
244 sure how that would work since some parts, like remembering what called it, is implicitly OO in
245 approach. This way it could be wrapped into CGI.pm and thus be much more widely used.
246  
247 I should also give some thought to a better approach to setting up tag parametres, and supporting
248 the sub-entity parametres as well (the DT, DD, and LI items).
249  
250 I'd love it if someone would help me find an easy way to get at the actual stuff of other types of
251 references, like displaying the first 160 characters of code in a PRE tag for a coderef, or some
252 easy way to list all the associated entities of a glob ref.
253  
254 Figure out that blasted blessed hashList issue. And remember the answer later.
255  
256 Figure out why all the Denny'ses are closing down.
257  
258 Figure out how to properly pluralise an inherently possessive proper noun.
259  
260 =head1 AUTHOR
261  
262 Dodger (Sean Cannon) dodger@dodger.org
263  
264 Contact me if you want to help or patch anything on the TO DO list.
265  
266 =head1 LICENCE
267  
268 Umm, call it the Artistic Licence. I hate the legal crap part. Dammit, Jim, I'm a programmer,
269 not a lwayer.
270  
271 This module is implicitly and explicitly distributed under the Perl Artistic Licence.
272 All rights reserved, all rites reversed.
273  
274 =head1 SEE ALSO
275  
276 perl(1), CGI(3)
277  
278 =cut
↑ Summary Lister/test.pl »