File Coverage

blib/lib/Data/HTMLDumper/Output.pm
Criterion Covered Total %
statement 62 62 100.0
branch 2 2 100.0
condition n/a
subroutine 18 18 100.0
pod 13 16 81.2
total 95 98 96.9


\n"; } \n"; } @$array"; "; \n"; \n"; \n"; \n"; @$array
line stmt bran cond sub pod time code
1             package Data::HTMLDumper::Output;
2 10     10   119 use strict; use warnings;
  10     10   21  
  10         356  
  10         59  
  10         22  
  10         9704  
3              
4             our $VERSION = '0.01';
5              
6             sub new {
7 11     11 1 49 my $class = shift;
8 11         20 my $unused;
9 11         59 return bless \$unused, $class;
10             }
11              
12             sub output {
13 19     19 1 38 my $self = shift;
14 19         33 my $array = shift;
15              
16 19         41 local $" = "";
17 19         458 return "@$array";
18             }
19              
20             sub expression {
21 19     19 1 66 my $self = shift;
22 19         101 my %item = @_;
23              
24 19         501 return "$item{item}
\n";
25             }
26              
27             sub item_value {
28 45     45 0 313 my $self = shift;
29 45         80 my $item = shift;
30 45         77 my $hash_value = shift;
31              
32 45 100       142 if (defined $hash_value) { return "
$item
  26         589  
33 19         434 else { return "$item
34             }
35              
36             sub item_array {
37 11     11 1 21 my $self = shift;
38 11         21 my $array_text = shift;
39              
40 11         252 return $array_text;
41             }
42              
43             sub item_hash {
44 25     25 1 54 my $self = shift;
45 25         43 my $hash_text = shift;
46              
47 25         570 return $hash_text;
48             }
49              
50             sub item_object {
51 2     2 1 4 my $self = shift;
52 2         3 my $object_text = shift;
53              
54 2         47 return $object_text;
55             }
56              
57             sub item_inside_out_object {
58 1     1 0 2 my $self = shift;
59 1         2 my $do_text = shift;
60              
61 1         18 return $do_text;
62             }
63              
64             sub array {
65 10     10 1 21 my $self = shift;
66 10         19 my $array = shift;
67              
68 10         24 local $" = "";
69 10         309 return "
70             }
71              
72             sub array_empty {
73 1     1 1 24 return "
NO_ELEMENTS
74             }
75              
76             sub hash {
77 24     24 1 48 my $self = shift;
78 24         39 my $pairs = shift;
79              
80 24         40 local $" = "";
81 24         596 return "@$pairs\n";
82             }
83              
84             sub hash_empty {
85 1     1 1 26 return "
NO_PAIRS
86             }
87              
88             sub pair {
89 33     33 1 51 my $self = shift;
90 33         47 my $key = shift;
91 33         51 my $value = shift;
92              
93 33         843 return "\n
$key "
94             . "$value
\n
95             }
96              
97             sub object {
98 2     2 1 3 my $self = shift;
99 2         5 my $object = shift;
100 2         2 my $class = shift;
101              
102 2         62 return "\n" \n" \n"
103             . "$object
104             . " isa $class
105             . "
106             }
107              
108             sub inside_out_object {
109 1     1 0 26 my $self = shift;
110 1         3 my $do_content = shift;
111 1         2 my $class = shift;
112              
113 1         26 return "\n" \n" \n"
114             . "\n"
115             . "do{\($do_content)}\n"
116             . "
117             . " isa $class
118             . "
119             }
120              
121             sub string {
122 77     77 1 109 my $self = shift;
123 77         150 my $text = shift;
124              
125 77         145 $text =~ s/&/&/g;
126 77         137 $text =~ s/
127 77         109 $text =~ s/>/>/g;
128             # $text =~ s/"/????/g; # XXX this needs the html code for "
129 77         1731 return $text;
130             }
131              
132             1;
133              
134             =head1 NAME
135              
136             Data::HTMLDumper::Output - Provides the default output for Data::HTMLDumper
137              
138             =head1 SYNOPSIS
139              
140             use Data::HTMLDumper;
141             # This module will become your output formatter.
142              
143             =head1 DESCRIPTION
144              
145             This is not a class you need to use directly, but if you want to control
146             what the output of Data::HTMLDumper looks like it will interest you.
147              
148             Data::HTMLDumper uses Parse::RecDescent to parse the output of Data::Dumper.
149             At most stages, when its productions match corresponding methods of this
150             class are called. Those methods are listed below. By subclassing this
151             class and overriding methods, you can control the output's appearance.
152             You can achieve the same thing by replacing the class outright. In either
153             case, you must tell Data::HTMLDumper by calling its actions method:
154              
155             my $your_action_object = YourSubclass->new();
156             Data::HTMLDumper->actions($your_action_object);
157              
158             By using objects, you can save your own state, though this class does not.
159              
160             =head1 METHODS
161              
162             The following methods are available to generate output. The are described
163             with lists of their parameters and samples of their output. In all cases
164             (except the constructor), the first argument is the invocant, which is not
165             listed.
166              
167             =head2 new
168              
169             This method is useful only for subclasses which do not need to save state.
170             It blesses a scalar reference. It only exists so that object oriented
171             access is possible.
172              
173             =head2 output
174              
175             This is called once each time the top level rule matches. It receives an
176             array reference pointing to the text for each expression that matched. It
177             is here to serve calls to Dumper which have multiple references.
178             Mine just interpolates the array into a string (after locally setting
179             $" to "").
180              
181             =head2 expression
182              
183             This method is called when the top level rule matches. It receives the
184             invocant plus a hash of consumed text from the input. The keys in the hash
185             are SIGIL (usually, or perhaps always, $), ID_NAME (the name of the var
186             as in VAR3), and item (which is the text produced by the methods below
187             for the rest of the expression).
188              
189             My version looks like this:
190              
191             sub expression {
192             my $self = shift;
193             my %item = @_;
194              
195             return "$item{item}
\n";
196             }
197              
198             I won't show any more whole methods, but this shows how easy it is to
199             generate the output. Simply use the input data to build a string.
200             Return that string.
201              
202             =head1 item_value
203              
204             This should probably be item_simple_value, but I didn't want to type
205             that everywhere. It receives two things: a simple item (like a string, a
206             number, or undef) and a flag telling the item is a hash value or not
207             (true means it is, undefined means it's not).
208              
209             =head2 item_array
210              
211             This is called when an entire array is seen. Usually the output is
212             already generated, so my routine simply returns its second argument.
213              
214             =head2 item_hash
215              
216             This is just like item_array, but for hashes. Again, all I do is return
217             the second argument.
218              
219             =head2 item_object
220              
221             This is the third and final in the series. It receives the object text.
222             Mine returns the second argument.
223              
224             =head2 array
225              
226             This is called with an array reference listing the output for the items
227             which are the elements of an array. Mine puts them in a row:
228              
229            
230              
231             Remember that it receives an array reference.
232              
233             =head2 array_empty
234              
235             This is called without arguments when an array of the form [] is seen.
236             Do what you like. I give back this:
237              
238            
NO_ELEMENTS
239              
240             =head2 hash
241              
242             This is like array above, but it recieves an array of the output for the
243             key/value pairs in the hash. I just stringify it:
244              
245             "@$pairs\n";
246              
247             (but I reset $" locally to "", so no extra spaces separate the entries)
248              
249             =head2 hash_empty
250              
251             This is just like array_empty except for hashes like {}. I return:
252              
253            
NO_PAIRS
254              
255             =head2 pair
256              
257             This is called with a key and its value item, each time a pair is seen in
258             a hash. My output creates a new row (since this is a whole hash), putting
259             the key in the first column and a table around the value in the second.
260              
261             =head2 object
262              
263             This is called when a blessed reference is found. It receives the object
264             and the class into which it is blessed.
265              
266             =head2 string
267              
268             This is called whenever a string value is seen. The string is passed
269             in. For HTML output it is wise to replace any html characters with
270             their appropriate entities, for example:
271              
272             $text =~ s/&/&/g;
273              
274             After several such substitutions, I return the string.
275              
276             =cut
277