Tags/Output.pm

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