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