line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 PUBLIC METHODS - |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head3 new |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Title: new |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Args: [fn str], [fh FILEHANDLE] |
37
|
|
|
|
|
|
|
Return: L |
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
|
|
|
14
|
|
213
|
use strict; |
|
14
|
|
|
|
|
31
|
|
|
14
|
|
|
|
|
484
|
|
102
|
14
|
|
|
14
|
|
73
|
use base qw(Data::Stag::BaseHandler); |
|
14
|
|
|
|
|
28
|
|
|
14
|
|
|
|
|
1277
|
|
103
|
14
|
|
|
14
|
|
105
|
use Data::Stag::Util qw(rearrange); |
|
14
|
|
|
|
|
28
|
|
|
14
|
|
|
|
|
888
|
|
104
|
|
|
|
|
|
|
|
105
|
14
|
|
|
14
|
|
72
|
use vars qw($VERSION); |
|
14
|
|
|
|
|
25
|
|
|
14
|
|
|
|
|
10890
|
|
106
|
|
|
|
|
|
|
$VERSION="0.14"; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub init { |
109
|
57
|
|
|
57
|
0
|
122
|
my $self = shift; |
110
|
57
|
|
|
|
|
266
|
$self->init_writer(@_); |
111
|
57
|
|
|
|
|
363
|
$self->SUPER::init(); |
112
|
57
|
|
|
|
|
137
|
return; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub init_writer { |
116
|
57
|
|
|
57
|
0
|
98
|
my $self = shift; |
117
|
57
|
|
|
|
|
315
|
my ($file, $fh) = rearrange([qw(file fh)], @_); |
118
|
57
|
100
|
66
|
|
|
398
|
$fh = \*STDOUT unless $fh || $file; |
119
|
57
|
100
|
|
|
|
340
|
$self->fh($fh) if $fh; |
120
|
57
|
100
|
|
|
|
148
|
$self->file($file) if $file; |
121
|
57
|
|
|
|
|
104
|
return; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub file { |
125
|
3413
|
|
|
3413
|
1
|
3885
|
my $self = shift; |
126
|
3413
|
100
|
|
|
|
6503
|
if (@_) { |
127
|
1
|
|
|
|
|
2
|
$self->{_file} = shift; |
128
|
1
|
|
|
|
|
2
|
$self->{_fh} = undef; # undo default setting of fh |
129
|
|
|
|
|
|
|
} |
130
|
3413
|
|
|
|
|
5675
|
return $self->{_file}; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub fh { |
135
|
78
|
|
|
78
|
1
|
132
|
my $self = shift; |
136
|
78
|
100
|
|
|
|
263
|
$self->{_fh} = shift if @_; |
137
|
78
|
|
|
|
|
179
|
return $self->{_fh}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub did_i_open_fh { |
141
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
142
|
1
|
50
|
|
|
|
4
|
$self->{_did_i_open_fh} = shift if @_; |
143
|
1
|
|
|
|
|
5
|
return $self->{_did_i_open_fh}; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub is_buffered { |
148
|
3451
|
|
|
3451
|
0
|
3823
|
my $self = shift; |
149
|
3451
|
100
|
|
|
|
6666
|
$self->{_is_buffered} = shift if @_; |
150
|
3451
|
|
|
|
|
10549
|
return $self->{_is_buffered}; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub finish { |
154
|
59
|
|
|
59
|
0
|
107
|
my $self = shift; |
155
|
59
|
|
|
|
|
397
|
while ($self->depth) { |
156
|
0
|
|
|
|
|
0
|
$self->end_event; |
157
|
|
|
|
|
|
|
} |
158
|
59
|
100
|
|
|
|
197
|
if ($self->{_did_i_open_fh}) { |
159
|
1
|
|
|
|
|
4
|
$self->close_fh; |
160
|
|
|
|
|
|
|
} |
161
|
59
|
|
|
|
|
28093
|
return; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub close_fh { |
165
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
166
|
2
|
|
|
|
|
6
|
my $fh = $self->fh; |
167
|
2
|
50
|
|
|
|
5
|
if ($fh) { |
168
|
2
|
|
|
|
|
10
|
$fh->close; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub safe_fh { |
173
|
3412
|
|
|
3412
|
1
|
3745
|
my $self = shift; |
174
|
3412
|
|
|
|
|
4471
|
my $fh = $self->{_fh}; |
175
|
3412
|
|
|
|
|
4676
|
my $file = $self->{_file}; |
176
|
|
|
|
|
|
|
|
177
|
3412
|
100
|
100
|
|
|
7372
|
if ($file && !$fh) { |
178
|
1
|
|
50
|
|
|
9
|
$fh = |
179
|
|
|
|
|
|
|
FileHandle->new(">$file") || die("cannot open file: $file"); |
180
|
1
|
|
|
|
|
131
|
$self->fh($fh); |
181
|
1
|
|
|
|
|
6
|
$self->did_i_open_fh(1); |
182
|
|
|
|
|
|
|
} |
183
|
3412
|
|
|
|
|
5568
|
$fh; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub addtext { |
187
|
3412
|
|
|
3412
|
0
|
3957
|
my $self = shift; |
188
|
3412
|
|
|
|
|
3898
|
my $msg = shift; |
189
|
3412
|
|
|
|
|
6053
|
my $fh = $self->safe_fh; |
190
|
3412
|
|
|
|
|
6244
|
my $file = $self->file; |
191
|
|
|
|
|
|
|
|
192
|
3412
|
100
|
66
|
|
|
6434
|
if (!$self->is_buffered && $fh) { |
193
|
124
|
|
|
|
|
184
|
print $fh $msg; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
else { |
196
|
3288
|
100
|
|
|
|
6519
|
if (!$self->{_buffer}) { |
197
|
38
|
|
|
|
|
108
|
$self->{_buffer} = ''; |
198
|
|
|
|
|
|
|
} |
199
|
3288
|
|
|
|
|
4946
|
$self->{_buffer} .= $msg; |
200
|
|
|
|
|
|
|
} |
201
|
3412
|
|
|
|
|
8968
|
return; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
*write = \&addtext; |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub writef { |
206
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
207
|
0
|
|
|
|
|
0
|
my $fmtstr = shift; |
208
|
0
|
|
|
|
|
0
|
$self->addtext(sprintf($fmtstr, @_)); |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub popbuffer { |
212
|
38
|
|
|
38
|
0
|
70
|
my $self = shift; |
213
|
38
|
|
|
|
|
170
|
my $b = $self->{_buffer}; |
214
|
38
|
|
|
|
|
81
|
$self->{_buffer} = ''; |
215
|
38
|
|
|
|
|
342
|
return $b; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head2 use_color |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Usage - |
222
|
|
|
|
|
|
|
Returns - |
223
|
|
|
|
|
|
|
Args - |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=cut |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
sub use_color { |
228
|
693
|
|
|
693
|
1
|
756
|
my $self = shift; |
229
|
693
|
50
|
|
|
|
1274
|
if (@_) { |
230
|
0
|
|
|
|
|
0
|
$self->{_use_color} = shift; |
231
|
0
|
0
|
|
|
|
0
|
if ($self->{_use_color}) { |
232
|
0
|
|
|
|
|
0
|
require "Term/ANSIColor.pm"; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
} |
235
|
693
|
|
|
|
|
1870
|
return $self->{_use_color}; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub DESTROY { |
239
|
59
|
|
|
59
|
|
390
|
my $self = shift; |
240
|
59
|
|
|
|
|
385
|
$self->finish; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
1; |