Tags/Output.pm

filter: Statementstmt 96.1% 151 / 157 Branchbran 89.1% 41 / 46 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 27 / 27 Podpod 100% 7 / 7 totaltotal 93.3% 240 / 257 CC 29 SCAR 17.7
CC
29
Cov
93%
put
15.0
flush
5.0
_check_params
5.0
CRAP 5.9
↑ Summary Tags/Output/Raw.pm »
1 package Tags::Output;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 27 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 27 our $VERSION = 0.16;
11  
12 # Constructor.
13 60 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
14 60 my ($class, @params) = @_;
15  
16 # Create object.
17 60 my $self = bless {}, $class;
18  
19 # Get default parameters.
20 60 $self->_default_parameters;
21  
22 # Process params.
23 60 set_params($self, @params);
24  
25 # Check parameters to right values.
26 56 $self->_check_params;
27  
28 # Initialization.
29 53 $self->reset;
30  
31 # Object.
32 53 return $self;
33 }
34  
35 # Finalize Tags output.
36 2 ▶sub finalize {
Subroutine finalize: called CC=2 SCAR=6.9
Pod documented
37 2 my $self = shift;
38  
39 2 ▶ while (@{$self->{'printed_tags'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"printed_tags"};}) 3 2 5
40 3 $self->put(['e', $self->{'printed_tags'}->[0]]);
41 }
42  
43 2 return;
44 }
45  
46 # Flush tags in object.
47 75 ▶sub flush {
Subroutine flush: called CC=5 SCAR=16.2
Pod documented
48 75 my ($self, $reset_flag) = @_;
49  
50 75 my $ouf = $self->{'output_handler'};
51 75 my $ret;
52 75 ▶ if (ref $self->{'flush_code'} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'flush_code'} eq 'ARRAY') { } 4 71 75
53 4 $ret = join $self->{'output_sep'}, @{$self->{'flush_code'}};
54 } else {
55 71 $ret = $self->{'flush_code'};
56 }
57  
58 # Output callback.
59 75 $self->_process_callback(\$ret, 'output_callback');
60  
61 75 ▶ if ($ouf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ouf) 13 62 75
62 27 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 13 ▶ print {$ouf} $ret or err 'Cannot write to output handler.';
Branch: 1 branch
BranchTrueFalseTotal
print {$ouf;} $ret or err("Cannot write to output handler.") 13 0 13
64 13 undef $ret;
65 }
66  
67 # Reset.
68 75 ▶ if ($reset_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reset_flag) 0 75 75
69 0 $self->reset;
70 }
71  
72 # Return string.
73 75 return $ret;
74 }
75  
76 # Return array of opened elements.
77 8 ▶sub open_elements {
Subroutine open_elements: called CC=1 SCAR=0.0
Pod documented
78 8 my $self = shift;
79  
80 8 return @{$self->{'printed_tags'}};
81 }
82  
83 # Deprecated.
84 4 ▶sub open_tags {
Subroutine open_tags: called CC=1 SCAR=0.0
Pod documented
85 4 my $self = shift;
86  
87 4 warn "Method open_tags() is deprecated";
88  
89 4 return $self->open_elements;
90 }
91  
92 # Put tags code.
93 89 ▶sub put {
Subroutine put: called CC=15 SCAR=27.1
Pod documented
94 89 my ($self, @data) = @_;
95  
96 # For every data.
97 89 foreach my $tags_structure_ar (@data) {
98  
99 # Bad data.
100 280 ▶ if (ref $tags_structure_ar ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tags_structure_ar ne "ARRAY") 1 279 280
101 1 err 'Bad data.';
102 }
103  
104 # Input 'Tags' item callback.
105 279 ▶ if (defined $self->{'input_tags_item_callback'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"input_tags_item_callback"}) 8 271 279
106 8 $self->{'input_tags_item_callback'}->($tags_structure_ar)
107 }
108  
109 # Split to type and main tags structure.
110 279 my ($type, @tags_struct) = @{$tags_structure_ar};
111  
112 # Attributes.
113 279 ▶ if ($type eq 'a') {
Branch: 8 branches
BranchTrueFalseTotal
if ($type eq 'a') { } 31 248 279
elsif ($type eq 'b') { } 89 159 248
elsif ($type eq 'cd') { } 6 153 159
elsif ($type eq 'c') { } 11 142 153
elsif ($type eq 'd') { } 39 103 142
elsif ($type eq 'e') { } 83 20 103
elsif ($type eq 'i') { } 5 15 20
elsif ($type eq 'r') { } 13 2 15
114 31 $self->_check_arguments(\@tags_struct, 1, 2);
115 31 $self->_put_attribute(@tags_struct);
116  
117 # Begin of tag.
118 } elsif ($type eq 'b') {
119 89 $self->_check_arguments(\@tags_struct, 1, 1);
120 89 $self->_put_begin_of_tag(@tags_struct);
121  
122 # CData.
123 } elsif ($type eq 'cd') {
124 6 $self->_put_cdata(@tags_struct);
125  
126 # Comment.
127 } elsif ($type eq 'c') {
128 11 $self->_put_comment(@tags_struct);
129  
130 # Data.
131 } elsif ($type eq 'd') {
132 39 $self->_put_data(@tags_struct);
133  
134 # End of tag.
135 } elsif ($type eq 'e') {
136 83 $self->_check_arguments(\@tags_struct, 1, 1);
137 83 $self->_put_end_of_tag(@tags_struct);
138  
139 # Instruction.
140 } elsif ($type eq 'i') {
141 5 ▶ if ($self->{'strict_instruction'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict_instruction"}) 5 0 5
142 5 $self->_check_arguments(\@tags_struct, 1, 2);
143 }
144 5 $self->_put_instruction(@tags_struct);
145  
146 # Raw data.
147 } elsif ($type eq 'r') {
148 13 $self->_put_raw(@tags_struct);
149  
150 # Other.
151 } else {
152 2 ▶ if (! $self->{'skip_bad_tags'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"skip_bad_tags"}) 1 1 2
153 1 err 'Bad type of data.';
154 }
155 }
156 }
157  
158 # Auto-flush.
159 83 ▶ if ($self->{'auto_flush'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auto_flush"}) 12 71 83
160 12 $self->flush;
161 12 $self->_reset_flush;
162 }
163  
164 83 return;
165 }
166  
167 # Reset.
168 5 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
169 5 my $self = shift;
170  
171 # Flush code.
172 5 $self->_reset_flush;
173  
174 # Printed tags.
175 5 $self->{'printed_tags'} = [];
176  
177 5 return;
178 }
179  
180 # Check arguments.
181 208 ▶sub _check_arguments {
Subroutine _check_arguments: called CC=3 SCAR=13.0
182 208 my ($self, $tags_struct_ar, $min_arg_num, $max_arg_num) = @_;
183  
184 208 my $arg_num = scalar @{$tags_struct_ar};
185 208 ▶ if ($arg_num < $min_arg_num || $arg_num > $max_arg_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_num < $min_arg_num or $arg_num > $max_arg_num) 0 208 208
Condition33%1/3
$arg_num < $min_arg_num or $arg_num > $max_arg_num
l!l&&r!l&&!r
00208
MC/DC0%0/2
$arg_num < $min_arg_num or $arg_num > $max_arg_num
$arg_num < $min_arg_num $arg_num > $max_arg_num
Truth table33%1/3
A or B
A= $arg_num < $min_arg_numB= $arg_num > $max_arg_num
ABresult
1X1
011
000
186 err 'Bad number of arguments.',
187 0 '\'Tags\' structure', join ', ', @{$tags_struct_ar};
188 }
189  
190 208 return;
191 }
192  
193 # Check parameters to rigth values.
194 56 ▶sub _check_params {
Subroutine _check_params: called CC=5 SCAR=16.1
195 56 my $self = shift;
196  
197 # Check to output handler.
198 56 ▶ if (defined $self->{'output_handler'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"output_handler"} and ref $self->{"output_handler"} ne "GLOB") 1 55 56
Condition100%3/3
defined $self->{"output_handler"} and ref $self->{"output_handler"} ne "GLOB"
!ll&&!rl&&r
5051
MC/DC100%2/2
defined $self->{"output_handler"} and ref $self->{"output_handler"} ne "GLOB"
defined $self->{"output_handler"} ref $self->{"output_handler"} ne "GLOB"
Truth table100%3/3
A and B
A= defined $self->{"output_handler"}B= ref $self->{"output_handler"} ne "GLOB"
ABresult
0X0
100
111
199 && ref $self->{'output_handler'} ne 'GLOB') {
200  
201 1 err 'Output handler is bad file handler.';
202 }
203  
204 # Check auto-flush only with output handler.
205 55 ▶ if ($self->{'auto_flush'} && ! defined $self->{'output_handler'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auto_flush"} and not defined $self->{"output_handler"}) 1 54 55
Condition100%3/3
$self->{"auto_flush"} and not defined $self->{"output_handler"}
!ll&&!rl&&r
5041
MC/DC100%2/2
$self->{"auto_flush"} and not defined $self->{"output_handler"}
$self->{"auto_flush"} not defined $self->{"output_handler"}
Truth table100%3/3
A and not(B)
A= $self->{"auto_flush"}B= not defined $self->{"output_handler"}
ABresult
0X0
100
111
206 1 err 'Auto-flush can\'t use without output handler.';
207 }
208  
209 54 return;
210 }
211  
212 # Default parameters.
213 60 ▶sub _default_parameters {
Subroutine _default_parameters: called CC=1 SCAR=0.1
214 60 my $self = shift;
215  
216 # Auto-flush.
217 60 $self->{'auto_flush'} = 0;
218  
219 # Input 'Tags' item callback.
220 60 $self->{'input_tags_item_callback'} = undef;
221  
222 # Output callback.
223 75 ▶ $self->{'output_callback'} = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
224 75 my ($data_sr, $self) = @_;
225  
226 75 ▶ if (defined $self->{'output_encoding'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"output_encoding"}) 0 75 75
227 0 ${$data_sr} = Encode::encode(
228 $self->{'output_encoding'},
229 0 ${$data_sr},
230 );
231 }
232  
233 75 return;
234 60 };
235  
236 # Output encoding.
237 60 $self->{'output_encoding'} = undef;
238  
239 # Set output handler.
240 60 $self->{'output_handler'} = undef;
241  
242 # Output separator.
243 60 $self->{'output_sep'} = "\n";
244  
245 # Skip bad tags.
246 60 $self->{'skip_bad_tags'} = 0;
247  
248 # Strict instruction.
249 60 $self->{'strict_instruction'} = 1;
250  
251 60 return;
252 }
253  
254 # Process dala callback.
255 151 ▶sub _process_callback {
Subroutine _process_callback: called CC=3 SCAR=11.0
256 151 my ($self, $data_r, $callback_type) = @_;
257  
258 # Process data callback.
259 151 ▶ if (defined $self->{$callback_type}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$callback_type} and ref $self->{$callback_type} eq "CODE") 139 12 151
Condition66%2/3
defined $self->{$callback_type} and ref $self->{$callback_type} eq "CODE"
!ll&&!rl&&r
120139
MC/DC50%1/2
defined $self->{$callback_type} and ref $self->{$callback_type} eq "CODE"
defined $self->{$callback_type} ref $self->{$callback_type} eq "CODE"
Truth table66%2/3
A and B
A= defined $self->{$callback_type}B= ref $self->{$callback_type} eq "CODE"
ABresult
0X0
100
111
260 && ref $self->{$callback_type} eq 'CODE') {
261  
262 139 $self->{$callback_type}->($data_r, $self);
263 }
264  
265 151 return;
266 }
267  
268 # Attributes.
269 2 ▶sub _put_attribute {
Subroutine _put_attribute: called CC=1 SCAR=0.0
270 2 my ($self, $attr, $value) = @_;
271  
272 2 push @{$self->{'flush_code'}}, 'Attribute';
273  
274 2 return;
275 }
276  
277 # Begin of tag.
278 5 ▶sub _put_begin_of_tag {
Subroutine _put_begin_of_tag: called CC=1 SCAR=0.0
279 5 my ($self, $tag) = @_;
280  
281 5 push @{$self->{'flush_code'}}, 'Begin of tag';
282  
283 5 unshift @{$self->{'printed_tags'}}, $tag;
284  
285 5 return;
286 }
287  
288 # CData.
289 2 ▶sub _put_cdata {
Subroutine _put_cdata: called CC=1 SCAR=0.0
290 2 my ($self, @cdata) = @_;
291  
292 2 push @{$self->{'flush_code'}}, 'CData';
293  
294 2 return;
295 }
296  
297 # Comment.
298 2 ▶sub _put_comment {
Subroutine _put_comment: called CC=1 SCAR=0.0
299 2 my ($self, @comments) = @_;
300  
301 2 push @{$self->{'flush_code'}}, 'Comment';
302  
303 2 return;
304 }
305  
306 # Data.
307 4 ▶sub _put_data {
Subroutine _put_data: called CC=1 SCAR=0.0
308 4 my ($self, @data) = @_;
309  
310 4 push @{$self->{'flush_code'}}, 'Data';
311  
312 4 return;
313 }
314  
315 # End of tag.
316 5 ▶sub _put_end_of_tag {
Subroutine _put_end_of_tag: called CC=1 SCAR=0.0
317 5 my ($self, $tag) = @_;
318  
319 5 push @{$self->{'flush_code'}}, 'End of tag';
320  
321 5 shift @{$self->{'printed_tags'}};
322  
323 5 return;
324 }
325  
326 # Instruction.
327 2 ▶sub _put_instruction {
Subroutine _put_instruction: called CC=1 SCAR=0.0
328 2 my ($self, $target, $code) = @_;
329  
330 2 push @{$self->{'flush_code'}}, 'Instruction';
331  
332 2 return;
333 }
334  
335 # Raw data.
336 2 ▶sub _put_raw {
Subroutine _put_raw: called CC=1 SCAR=0.0
337 2 my ($self, @raw_data) = @_;
338  
339 2 push @{$self->{'flush_code'}}, 'Raw data';
340  
341 2 return;
342 }
343  
344 # Reset flush code.
345 5 ▶sub _reset_flush {
Subroutine _reset_flush: called CC=1 SCAR=0.0
346 5 my $self = shift;
347  
348 5 $self->{'flush_code'} = [];
349  
350 5 return;
351 }
352  
353 27 1;
354  
355 __END__
↑ Summary Tags/Output/Raw.pm »