Treex/Block/Write/BaseTextWriter.pm

filter: Statementstmt 95.1% 39 / 41 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 83.5% 56 / 67 CC 5 SCAR 9.2
CC
5
Cov
84%
_build_to
6.0
__ANON__
2.3
BEGIN
1.0
CRAP 2.5
« Treex/Block/Util/SetGlobal.pm ↑ Summary Treex/Core/TredView/Common.pm »
1 package Treex::Block::Write::BaseTextWriter;
2 4 $Treex::Block::Write::BaseTextWriter::VERSION = '2.20151210';
3 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Encode 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 extends 'Treex::Block::Write::BaseWriter';
9  
10 4 has encoding => (
11 isa => 'Str',
12 is => 'ro',
13 default => 'utf8',
14 documentation => 'Output encoding. \'utf8\' by default.',
15 );
16  
17 4 has '+to' => (
18 isa => 'Maybe[Str]',
19 builder => '_build_to',
20 lazy_build => 1
21 );
22  
23 4 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 1 ▶ my ( $orig, $self, $filename ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32 # actually open the file handle
33 1 my $handle = $self->$orig($filename);
34  
35 # set the right encoding
36 1 binmode( $handle, ':' . $self->encoding );
37 1 return $handle;
38 4 };
39  
40 # Default to standard output if no output file is set.
41 sub _build_to {
42  
43 1 ▶ my ($self) = @_;
Subroutine _build_to: called CC=4 SCAR=17.9
44  
45 1 ▶ if ( !defined( $self->path ) && !defined( $self->file_stem ) && !defined( $self->substitute ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->path or defined $self->file_stem or defined $self->substitute) 1 0 1
Condition33%2/6
defined $self->path or defined $self->file_stem or defined $self->substitute
l!l&&r!l&&!r
001
defined $self->path or defined $self->file_stem
l!l&&r!l&&!r
001
MC/DC0%0/3
defined $self->path or defined $self->file_stem or defined $self->substitute
defined $self->path defined $self->file_stem defined $self->substitute
Truth table25%1/4
A or B or C
A= defined $self->pathB= defined $self->file_stemC= defined $self->substitute
ABCresult
1XX1
01X1
0011
0000
46 1 return '-';
47 }
48 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 1 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=2 SCAR=8.1
55  
56 1 ▶ return $self->to if ( $self->to !~ m/[ ,]/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->to =~ /[ ,]/ 1 0 1
57 0 return super();
58 4 };
59  
60 sub _do_before_process {
61 1 ▶ my ($self, $document) = @_;
Subroutine _do_before_process: called CC=1 SCAR=0.0
62 # allow header printing (in overrides)
63 1 $self->print_header($document);
64  
65 1 return;
66 }
67  
68 sub _do_after_process {
69 1 ▶ my ($self, $document) = @_;
Subroutine _do_after_process: called CC=1 SCAR=0.0
70  
71 # allow footer printing (in overrides)
72 1 $self->print_footer($document);
73  
74 1 return;
75 }
76  
77  
78 sub print_header {
79 1 ▶ my ( $self, $document ) = @_;
Subroutine print_header: called CC=1 SCAR=0.0
Pod documented
80 1 return;
81 }
82  
83 sub print_footer {
84 1 ▶ my ( $self, $document ) = @_;
Subroutine print_footer: called CC=1 SCAR=0.0
Pod documented
85 1 return;
86 }
87  
88 4 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.20151210
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.
« Treex/Block/Util/SetGlobal.pm ↑ Summary Treex/Core/TredView/Common.pm »