blib/lib/Data/Stag/Writer.pm

filter: Statementstmt 91.4% 75 / 82 Branchbran 80.0% 24 / 30 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 94.4% 17 / 18 Podpod 38.4% 5 / 13 totaltotal 82.6% 133 / 161 CC 20 SCAR 10.9
CC
20
Cov
87%
init_writer
5.0
use_color
4.9
safe_fh
4.0
CRAP 3.0
« blib/lib/Data/Stag/XSLHandler.pm ↑ Summary blib/lib/Data/Stag/XMLParser.pm »
1 package Data::Stag::Writer;
2  
3 =head1 NAME
4  
5 Data::Stag::Writer - base class for all Writers
6  
7 =head1 SYNOPSIS
8  
9 # Abstract class - do not use directly
10 package MyOutputter;
11 use base qw(Data::Stag::Writer);
12  
13 sub e_foo {
14 my ($self, $foo) = @_;
15 $self->writef("data1: %s\n", $foo->get_data1);
16 return;
17 }
18  
19  
20 =cut
21  
22 =head1 DESCRIPTION
23  
24 base mixin class for all writers
25  
26 =head1 INHERITANCE
27  
28 This inherits from L<Data::Stag::BaseHandler>
29  
30 =head1 PUBLIC METHODS -
31  
32 =head3 new
33  
34 Title: new
35  
36 Args: [fn str], [fh FILEHANDLE]
37 Return: L<Data::Stag::BaseHandler>
38 Example: $w = MyWriter->new(-fh=>$fh);
39  
40 returns the tree that was built from all uncaught events
41  
42 =head3 file
43  
44 Title: file
45  
46 Args: filename str
47 Returns: filename str
48 Example: $handler->file("my_output_file.txt");
49  
50 Setting this will cause all output to be diverted to this file; the
51 file will be overwritten by default. The filehandle will not be opened
52 unless any events are thrown
53  
54 For more fine-grained control, use $handler->fh()
55  
56 =head3 fh
57  
58 Title: fh
59  
60 Args: filehandle FH
61 Returns: filehandle FH
62 Example: $handler->fh(\*STDOUT);
63  
64 Gets/Sets the output filehandle for the writer
65  
66 =head3 safe_fh
67  
68 Title: safe_fh
69 Type: PROTECTED
70  
71 Args: filehandle FH
72 Returns: filehandle FH
73 Example: $handler->fh(\*STDOUT);
74  
75 As fh(), but makes sure that the filehandle is initialized
76  
77 You should use this if you are overriding this class
78  
79 =head3 write
80  
81 Title: write
82  
83 Type: PROTECTED
84 Args: message str
85 Returns:
86 Example: $self->write($stag->get_blah);
87  
88 writes output
89  
90 to be used by classes that subclass this one
91  
92 =head3 writef
93  
94 Title: writef
95  
96 As write, analagous to printf
97  
98  
99 =cut
100  
101 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 14 ▶use base qw(Data::Stag::BaseHandler);
Subroutine BEGIN: called CC=1 SCAR=0.0
103 14 ▶use Data::Stag::Util qw(rearrange);
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 14 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
106 14 $VERSION="0.14";
107  
108 57 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
109 57 my $self = shift;
110 57 $self->init_writer(@_);
111 57 $self->SUPER::init();
112 57 return;
113 }
114  
115 57 ▶sub init_writer {
Subroutine init_writer: called CC=5 SCAR=16.1
Pod undocumented
116 57 my $self = shift;
117 57 my ($file, $fh) = rearrange([qw(file fh)], @_);
118 57 ▶ $fh = \*STDOUT unless $fh || $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh or $file 56 1 57
Condition66%2/3
$fh or $file
l!l&&r!l&&!r
0156
MC/DC50%1/2
$fh or $file
$fh $file
Truth table66%2/3
A or B
A= $fhB= $file
ABresult
1X1
011
000
119 57 ▶ $self->fh($fh) if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 56 1 57
120 57 ▶ $self->file($file) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 1 56 57
121 57 return;
122 }
123  
124 3413 ▶sub file {
Subroutine file: called CC=2 SCAR=6.9
Pod documented
125 3413 my $self = shift;
126 3413 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 3412 3413
127 1 $self->{_file} = shift;
128 1 $self->{_fh} = undef; # undo default setting of fh
129 }
130 3413 return $self->{_file};
131 }
132  
133  
134 78 ▶sub fh {
Subroutine fh: called CC=2 SCAR=6.9
Pod documented
135 78 my $self = shift;
136 78 ▶ $self->{_fh} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 75 3 78
137 78 return $self->{_fh};
138 }
139  
140 1 ▶sub did_i_open_fh {
Subroutine did_i_open_fh: called CC=2 SCAR=7.1
Pod undocumented
141 1 my $self = shift;
142 1 ▶ $self->{_did_i_open_fh} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
143 1 return $self->{_did_i_open_fh};
144 }
145  
146  
147 3451 ▶sub is_buffered {
Subroutine is_buffered: called CC=2 SCAR=6.9
Pod undocumented
148 3451 my $self = shift;
149 3451 ▶ $self->{_is_buffered} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 39 3412 3451
150 3451 return $self->{_is_buffered};
151 }
152  
153 59 ▶sub finish {
Subroutine finish: called CC=3 SCAR=11.2
Pod undocumented
154 59 my $self = shift;
155 59 ▶ while ($self->depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->depth) 0 59 59
156 0 $self->end_event;
157 }
158 59 ▶ if ($self->{_did_i_open_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_did_i_open_fh"}) 1 58 59
159 1 $self->close_fh;
160 }
161 59 return;
162 }
163  
164 2 ▶sub close_fh {
Subroutine close_fh: called CC=2 SCAR=7.0
Pod undocumented
165 2 my $self = shift;
166 2 my $fh = $self->fh;
167 2 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 2 0 2
168 2 $fh->close;
169 }
170 }
171  
172 3412 ▶sub safe_fh {
Subroutine safe_fh: called CC=4 SCAR=13.9
Pod documented
173 3412 my $self = shift;
174 3412 my $fh = $self->{_fh};
175 3412 my $file = $self->{_file};
176  
177 3412 ▶ if ($file && !$fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and not $fh) 1 3411 3412
Condition100%3/3
$file and not $fh
!ll&&!rl&&r
3313981
MC/DC100%2/2
$file and not $fh
$file not $fh
Truth table100%3/3
A and not(B)
A= $fileB= not $fh
ABresult
0X0
100
111
178 1 ▶ $fh =
Condition50%1/2
'FileHandle'->new(">$file") || die("cannot open file: $file")
l!l
10
MC/DC0%0/1
'FileHandle'->new(">$file") || die("cannot open file: $file")
'FileHandle'->new(">$file")
Truth table50%1/2
A
A= 'FileHandle'->new(">$file")
Aresult
11
00
179 FileHandle->new(">$file") || die("cannot open file: $file");
180 1 $self->fh($fh);
181 1 $self->did_i_open_fh(1);
182 }
183 3412 $fh;
184 }
185  
186 3412 ▶sub addtext {
Subroutine addtext: called CC=4 SCAR=13.9
Pod undocumented
187 3412 my $self = shift;
188 3412 my $msg = shift;
189 3412 my $fh = $self->safe_fh;
190 3412 my $file = $self->file;
191  
192 3412 ▶ if (!$self->is_buffered && $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_buffered and $fh) { } 124 3288 3412
Condition66%2/3
not $self->is_buffered and $fh
!ll&&!rl&&r
32880124
MC/DC50%1/2
not $self->is_buffered and $fh
not $self->is_buffered $fh
Truth table66%2/3
not(A) and B
A= not $self->is_bufferedB= $fh
ABresult
0X0
100
111
193 124 print $fh $msg;
194 }
195 else {
196 3288 ▶ if (!$self->{_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_buffer"}) 38 3250 3288
197 38 $self->{_buffer} = '';
198 }
199 3288 $self->{_buffer} .= $msg;
200 }
201 3412 return;
202 }
203 14 *write = \&addtext;
204  
205 0 ▶sub writef {
Subroutine writef: not called CC=1 SCAR=6.9
Pod documented
206 0 my $self = shift;
207 0 my $fmtstr = shift;
208 0 $self->addtext(sprintf($fmtstr, @_));
209 }
210  
211 38 ▶sub popbuffer {
Subroutine popbuffer: called CC=1 SCAR=0.0
Pod undocumented
212 38 my $self = shift;
213 38 my $b = $self->{_buffer};
214 38 $self->{_buffer} = '';
215 38 return $b;
216 }
217  
218  
219 =head2 use_color
220  
221 Usage -
222 Returns -
223 Args -
224  
225 =cut
226  
227 693 ▶sub use_color {
Subroutine use_color: called CC=3 SCAR=16.0
Pod documented
228 693 my $self = shift;
229 693 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 693 693
230 0 $self->{_use_color} = shift;
231 0 ▶ if ($self->{_use_color}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_use_color"}) 0 0 0
232 0 require "Term/ANSIColor.pm";
233 }
234 }
235 693 return $self->{_use_color};
236 }
237  
238 59 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
239 59 my $self = shift;
240 59 $self->finish;
241 }
242  
243 14 1;
« blib/lib/Data/Stag/XSLHandler.pm ↑ Summary blib/lib/Data/Stag/XMLParser.pm »