File Coverage

blib/lib/Treex/Block/Write/BaseTextWriter.pm
Criterion Covered Total %
statement 25 26 96.1
branch 1 2 50.0
condition 2 6 33.3
subroutine 9 9 100.0
pod 2 2 100.0
total 39 45 86.6


line stmt bran cond sub pod time code
1             package Treex::Block::Write::BaseTextWriter;
2             $Treex::Block::Write::BaseTextWriter::VERSION = '2.20210102';
3 1     1   604 use Moose;
  1         4  
  1         7  
4 1     1   6698 use Treex::Core::Common;
  1         3  
  1         8  
5 1     1   5857 use autodie;
  1         5  
  1         9  
6 1     1   5768 use Encode 'decode';
  1         3  
  1         680  
7              
8             extends 'Treex::Block::Write::BaseWriter';
9              
10             has encoding => (
11             isa => 'Str',
12             is => 'ro',
13             default => 'utf8',
14             documentation => 'Output encoding. \'utf8\' by default.',
15             );
16              
17             has '+to' => (
18             isa => 'Maybe[Str]',
19             builder => '_build_to',
20             lazy_build => 1
21             );
22              
23             has '+compress' => (
24             default => 0
25             );
26              
27             # Set the right text encoding when opening a handle.
28             around '_open_file_handle' => sub {
29              
30             my ( $orig, $self, $filename ) = @_;
31              
32             # actually open the file handle
33             my $handle = $self->$orig($filename);
34              
35             # set the right encoding
36             binmode( $handle, ':' . $self->encoding );
37             return $handle;
38             };
39              
40             # Default to standard output if no output file is set.
41             sub _build_to {
42              
43 1     1   3 my ($self) = @_;
44              
45 1 50 33     36 if ( !defined( $self->path ) && !defined( $self->file_stem ) && !defined( $self->substitute ) ) {
      33        
46 1         32 return '-';
47             }
48 0         0 return;
49             }
50              
51             # Append everything to one file if the 'to' parameter is set just to one file.
52             override '_get_next_filename' => sub {
53              
54             my ($self) = @_;
55              
56             return $self->to if ( $self->to !~ m/[ ,]/ );
57             return super();
58             };
59              
60             sub _do_before_process {
61 1     1   5 my ($self, $document) = @_;
62             # allow header printing (in overrides)
63 1         9 $self->print_header($document);
64              
65 1         2 return;
66             }
67              
68             sub _do_after_process {
69 1     1   4 my ($self, $document) = @_;
70              
71             # allow footer printing (in overrides)
72 1         7 $self->print_footer($document);
73              
74 1         3 return;
75             }
76              
77              
78             sub print_header {
79 1     1 1 4 my ( $self, $document ) = @_;
80 1         3 return;
81             }
82              
83             sub print_footer {
84 1     1 1 4 my ( $self, $document ) = @_;
85 1         2 return;
86             }
87              
88             1;
89              
90             __END__
91              
92             =encoding utf-8
93              
94             =head1 NAME
95              
96             Treex::Block::Write::BaseTextWriter
97              
98             =head1 VERSION
99              
100             version 2.20210102
101              
102             =head1 DESCRIPTION
103              
104             This is a base class for all text-based output formats, which adds printing to standard output
105             by default and the possibility to select the output file character encoding (defaulting to
106             UTF-8).
107              
108             Also, if multiple documents are read and only one output file given in the C<to> parameter,
109             all input documents will be appended to a single file.
110              
111             =head1 PARAMETERS
112              
113             =over
114              
115             =item C<encoding>
116              
117             The output encoding, C<utf8> by default.
118              
119             =back
120              
121             =head1 DERIVED CLASSES
122              
123             Before creating a class derived from C<BaseTextWriter>, please see the instructions in
124             L<Treex::Block::Write::BaseWriter>.
125              
126             There is a possibility to override the following two methods in addition to those described in
127             L<Treex::Block::Write::BaseWriter>:
128              
129             =over
130              
131             =item C<<$self->print_header($document)>>
132              
133             Print a document header. Called before the standard C<process_document> is launched. Will not be
134             called if C<process_document> is overridden (and C<super> not called)!
135              
136             =item C<<$self->print_footer($document)>>
137              
138             Print a document footer. Called after the standard C<process_document> is launched. Will not be
139             called if C<process_document> is overridden (and C<super> not called)!
140              
141             =back
142              
143             =head1 AUTHOR
144              
145             Ondřej Dušek <odusek@ufal.mff.cuni.cz>
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             Copyright © 2011-2012 by Institute of Formal and Applied Linguistics, Charles University in Prague
150              
151             This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.