TT/IO.pm

filter: Statementstmt 23.5% 25 / 106 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 27.5% 8 / 29 Podpod 0.0% 0 / 21 totaltotal 15.0% 33 / 220 CC 27 SCAR 36.7
CC
27
Cov
15%
open
156.0
getSentence
20.0
close
12.0
CRAP 39.3
« TT/Persistent.pm ↑ Summary TT/Packed/at.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: Lingua::TT::IO.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Descript: TT I/O: file I/O object
5  
6  
7 package Lingua::TT::IO;
8 1 ▶use Lingua::TT::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Lingua::TT::Sentence;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Lingua::TT::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 ##==============================================================================
20 ## Globals & Constants
21  
22 #==============================================================================
23 ## Constructors etc.
24  
25 ## $io = CLASS_OR_OBJECT->new(%opts)
26 ## + $io: HASH-ref
27 ## (
28 ## encoding => $encoding, ##-- I/O encoding (default: undef)
29 ## ## + uses perlio :encoding($encoding) layer
30 ## ## + undef (default) uses perl default (locale?)
31 ## ## + undef gives fastest I/O, since no translation is required
32 ## ## + pseudo-encoding 'raw' sets ':raw' layer
33 ## ## + false encodings (0,'') set no perlIO flags at all
34 ## fh => $fh, ##-- underlying filehandle (default: none)
35 ## name => $name, ##-- source name to use for error reporting (defualt: none)
36 ## )
37 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
38 0 my $that = shift;
39 0 ▶ return bless({
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
40 encoding => undef,
41 fh => undef,
42 name => undef,
43 @_,
44 }, ref($that)||$that);
45 }
46  
47 #x undef = $io->DESTROY()
48 #x + destructor implicitly calls close()
49 #sub DESTROY {
50 # $_[0]->close();
51 #}
52  
53 ##==============================================================================
54 ## Methods: I/O: Generic
55  
56 ## $io = $io->close()
57 ## + closes $io object, if open
58 0 ▶sub close {
Subroutine close: not called CC=3 SCAR=24.8
Pod undocumented
59 0 my $io = shift;
60 0 ▶ return $io if (!ref($io));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $io 0 0 0
61 0 ▶ $io->{fh}->close if (defined($io->{fh}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $io->{"fh"} 0 0 0
62 0 delete(@$io{qw(fh name)});
63 0 return $io;
64 }
65  
66 ## $io = CLASS_OR_OBJECT->open($mode,$src,%opts)
67 ## + opens $io with $mode on $src
68 ## + %opts: clobbers %$io (e.g. encoding=>$encoding)
69 0 ▶sub open {
Subroutine open: not called CC=12 SCAR=50.5
Pod undocumented
70 0 my ($io,$mode,$src,%opts) = @_;
71 0 ▶ $io = $io->new if (!ref($io));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $io 0 0 0
72 0 $io->close();
73 0 @$io{'name',keys(%opts)} = ($src,values(%opts));
74 0 ▶ if (ref($src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src) { } 0 0 0
75 0 ▶ $mode .= '&' if (UNIVERSAL::isa($src,'IO::Handle') && $mode !~ /\&/);
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($src, "IO::Handle") and not $mode =~ /\&/ 0 0 0
Condition0%0/3
UNIVERSAL::isa($src, "IO::Handle") and not $mode =~ /\&/
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($src, "IO::Handle") and not $mode =~ /\&/
UNIVERSAL::isa($src, "IO::Handle") not $mode =~ /\&/
Truth table0%0/3
A and not(B)
A= UNIVERSAL::isa($src, "IO::Handle")B= not $mode =~ /\&/
ABresult
0X0
100
111
76 0 ▶ $io->{fh} = IO::Handle->new() if (!defined($io->{fh}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $io->{"fh"} 0 0 0
77 0 ▶ CORE::open($io->{fh},$mode,$src)
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $io->{"fh"}, $mode, $src or confess(ref($io) . qq[::open(): open failed with mode '${mode}' for '$io->{"name"}': $!]) 0 0 0
78 or confess(ref($io)."::open(): open failed with mode '$mode' for '$io->{name}': $!");
79 } else {
80 0 ▶ $io->{fh} = IO::File->new($mode.$src)
Branch: 1 branch
BranchTrueFalseTotal
$io->{"fh"} = "IO::File"->new($mode . $src) or confess(ref($io) . "::open(): open failed with mode '${mode}' for file '${src}': $!") 0 0 0
81 or confess(ref($io)."::open(): open failed with mode '$mode' for file '$src': $!");
82 }
83 0 ▶ if ($io->{encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io->{"encoding"}) 0 0 0
84 0 ▶ if ($io->{encoding} eq 'raw') {
Branch: 2 branches
BranchTrueFalseTotal
if ($io->{'encoding'} eq 'raw') { } 0 0 0
elsif ($io->{'encoding'} eq 'utf8' or $io->{'encoding'} eq 'UTF-8') { } 0 0 0
85 0 binmode($io->{fh}, ':raw');
86 }
87 ▶ elsif ($io->{encoding} eq 'utf8' || $io->{encoding} eq 'UTF-8') {
Condition0%0/3
$io->{"encoding"} eq "utf8" or $io->{"encoding"} eq "UTF-8"
l!l&&r!l&&!r
000
MC/DC0%0/2
$io->{"encoding"} eq "utf8" or $io->{"encoding"} eq "UTF-8"
$io->{"encoding"} eq "utf8" $io->{"encoding"} eq "UTF-8"
Truth table0%0/3
A or B
A= $io->{"encoding"} eq "utf8"B= $io->{"encoding"} eq "UTF-8"
ABresult
1X1
011
000
88 0 binmode($io->{fh}, ':utf8');
89 }
90 else {
91 0 binmode($io->{fh},":encoding($io->{encoding})");
92 }
93 }
94 0 return $io;
95 }
96  
97 ## $bool = $io->opened()
98 ## + returns true iff $io is opened
99 0 ▶sub opened {
Subroutine opened: not called CC=1 SCAR=6.9
Pod undocumented
100 0 return defined($_[0]{fh});
101 }
102  
103 ## $bool = $io->eof()
104 ## + returns true iff $io->{fh} is at eof (or closed)
105 0 ▶sub eof {
Subroutine eof: not called CC=2 SCAR=17.9
Pod undocumented
106 0 ▶ return !defined($_[0]{fh}) || CORE::eof($_[0]{fh});
Condition0%0/3
!defined($_[0]{'fh'}) || CORE::eof $_[0]{'fh'}
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($_[0]{'fh'}) || CORE::eof $_[0]{'fh'}
!defined($_[0]{'fh'}) CORE::eof $_[0]{'fh'}
Truth table0%0/3
not(A) || B
A= !defined($_[0]{'fh'})B= CORE::eof $_[0]{'fh'}
ABresult
1X1
011
000
107 }
108  
109 ##----------------------------------------------------------------------
110 ## Methods: I/O: Generic: Input
111  
112 ## $io = CLASS_OR_OBJECT->fromFile($filename_or_fh,%opts)
113 ## + opens $io for read from $filename_or_fh
114 0 ▶sub fromFile {
Subroutine fromFile: not called CC=2 SCAR=17.9
Pod undocumented
115 0 ▶ return $_[0]->open('<&',@_[1..$#_]) if (ref($_[1]));
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] 0 0 0
116 0 return $_[0]->open('<', @_[1..$#_]);
117 }
118  
119 ## $io = CLASS_OR_OBJECT->fromFh($fh,%opts)
120 ## + opens $io for read from dup of $fh
121 0 ▶sub fromFh {
Subroutine fromFh: not called CC=1 SCAR=6.9
Pod undocumented
122 0 return $_[0]->open('<&',@_[1..$#_]);
123 }
124  
125 ## $io = CLASS_OR_OBJECT->fromString(\$string,%opts)
126 ## + opens $io for read from string $string
127 0 ▶sub fromString {
Subroutine fromString: not called CC=1 SCAR=6.9
Pod undocumented
128 0 return $_[0]->open('<',@_[1..$#_]);
129 }
130  
131 ##----------------------------------------------------------------------
132 ## Methods: I/O: Generic: Output
133  
134 ## $io = CLASS_OR_OBJECT->toFile($filename_or_fh,%opts)
135 ## + opens $io for write (clobbering) to $filename_or_fh
136 0 ▶sub toFile {
Subroutine toFile: not called CC=2 SCAR=17.9
Pod undocumented
137 0 ▶ return $_[0]->open('>&',@_[1..$#_]) if (ref($_[1]));
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] 0 0 0
138 0 return $_[0]->open('>', @_[1..$#_]);
139 }
140  
141 ## $io = CLASS_OR_OBJECT->toFh($fh,%opts)
142 ## + opens $io for write to dup of $fh
143 0 ▶sub toFh {
Subroutine toFh: not called CC=1 SCAR=6.9
Pod undocumented
144 0 return $_[0]->open('>&',@_[1..$#_]);
145 }
146  
147 ## $io = CLASS_OR_OBJECT->toString(\$string,%opts)
148 ## + opens $io for write (clobbering) to string $string
149 0 ▶sub toString {
Subroutine toString: not called CC=1 SCAR=6.9
Pod undocumented
150 0 return $_[0]->open('>',@_[1..$#_]);
151 }
152  
153 ##==============================================================================
154 ## Methods: I/O: Input
155  
156 ## $tok_or_undef = $io->getToken()
157 ## + gets next (possibly empty) token from input stream
158 ## + returns undef of EOF
159 0 ▶sub getToken {
Subroutine getToken: not called CC=2 SCAR=17.9
Pod undocumented
160 0 my $io = shift;
161 0 ▶ return undef if ($io->eof);
Branch: 1 branch
BranchTrueFalseTotal
if $io->eof 0 0 0
162 0 return bless([split(/[\n\r]*[\t\n\r][\n\r]*/,$io->{fh}->getline)], 'Lingua::TT::Token');
163 }
164  
165 ## $sent_or_undef = $io->getSentence()
166 ## + gets next (possibly empty) sentence from input stream
167 ## + returns undef of EOF
168 0 ▶sub getSentence {
Subroutine getSentence: not called CC=4 SCAR=30.0
Pod undocumented
169 0 my $io = shift;
170 0 ▶ return undef if ($io->eof);
Branch: 1 branch
BranchTrueFalseTotal
if $io->eof 0 0 0
171 0 my $sent = bless([],'Lingua::TT::Sentence');
172 0 my ($line);
173 0 ▶ while (defined($line=$io->{fh}->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $io->{"fh"}->getline)) 0 0 0
174 0 ▶ last if ($line =~ /^\r?$/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\r?$/ 0 0 0
175 0 push(@$sent, bless([split(/[\n\r]*[\t\n\r][\n\r]*/,$line)], 'Lingua::TT::Token'));
176 }
177 0 return $sent;
178 }
179  
180 ## $doc_or_undef = $io->getDocument()
181 ## + gets all remaining data from input stream as a Lingua::TT:Document
182 ## + returns undef of EOF
183 0 ▶sub getDocument {
Subroutine getDocument: not called CC=2 SCAR=17.9
Pod undocumented
184 0 my $io = shift;
185 0 ▶ return undef if ($io->eof);
Branch: 1 branch
BranchTrueFalseTotal
if $io->eof 0 0 0
186 0 my $fh = $io->{fh};
187 0 my ($buf);
188 {
189 0 local $/ = undef;
190 0 $buf = <$fh>;
191 }
192 0 my $doc = bless([],'Lingua::TT::Document');
193 @$doc = map {
194 0 bless([
195 map {
196 0 bless([split(/[\n\r]*[\t\n\r][\n\r]*/,$_)], 'Lingua::TT::Token')
197 } split(/[\r\n]+/,$_)
198 ],
199 'Lingua::TT::Sentence')
200 } split(/(?:\r?\n){2}/, $buf);
201  
202 0 return $doc;
203 }
204  
205 ## $line = $io->getLine();
206 ## + gets a single line from input stream, or undef on eof
207 ## + basically just a wrapper for the <> operator
208 0 ▶sub getLine {
Subroutine getLine: not called CC=1 SCAR=6.9
Pod undocumented
209 0 my $fh = $_[0]{fh};
210 0 return scalar(<$fh>);
211 }
212  
213 ## \@lines = $io->getLines(); ##-- scalar context
214 ## @lines = $io->getLines(); ##-- array context
215 ## + gets all remaining data from input stream as a flat list of (decoded) strings
216 ## + returns undef of EOF
217 0 ▶sub getLines {
Subroutine getLines: not called CC=3 SCAR=24.8
Pod undocumented
218 0 my $io = shift;
219 0 ▶ return undef if ($io->eof);
Branch: 1 branch
BranchTrueFalseTotal
if $io->eof 0 0 0
220 0 my $fh = $io->{fh};
221 0 my ($buf);
222 {
223 0 local $/ = undef;
224 0 $buf = scalar(<$fh>);
225 }
226 0 my $lines = [split(/\r?\n/, $buf)];
227 0 ▶ return wantarray ? @$lines : $lines;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
228 }
229  
230  
231 ##==============================================================================
232 ## Methods: I/O: Output
233  
234 ## $io = $io->putToken($tok)
235 ## + writes $tok to output stream
236 0 ▶sub putToken {
Subroutine putToken: not called CC=1 SCAR=6.9
Pod undocumented
237 0 $_[0]{fh}->print(join("\t", @{$_[1]})."\n");
238 }
239  
240 ## $io = $io->putSentence($sent)
241 ## + writes $sent to output stream
242 0 ▶sub putSentence {
Subroutine putSentence: not called CC=1 SCAR=6.9
Pod undocumented
243 0 $_[0]{fh}->print(join("\n", map {join("\t",@$_)} @{$_[1]})."\n\n");
244 }
245  
246 ## $io = $io->putDocument($doc)
247 ## + writes $doc to output stream
248 0 ▶sub putDocument {
Subroutine putDocument: not called CC=1 SCAR=6.9
Pod undocumented
249 0 $_[0]{fh}->print(map { join("\n",map {join("\t",@$_)} @$_)."\n\n" } @{$_[1]});
250 }
251  
252 ## undef = $io->print(@data)
253 ## + wrapper for $io->{fh}->print(@data)
254 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
Pod undocumented
255 0 $_[0]{fh}->print(@_[1..$#_]);
256 }
257  
258 ## $io = $io->putLines(@lines);
259 ## $io = $io->putLines(\@lines);
260 ## + dumps @lines to output handle, adding terminating newline
261 0 ▶sub putLines {
Subroutine putLines: not called CC=3 SCAR=24.8
Pod undocumented
262 0 my $io = shift;
263 0 ▶ $io->{fh}->print(map {/\R\z/ ? $_ : "$_\n"} (ref($_[0]) ? @{$_[0]} : @_));
Branch: 2 branches
BranchTrueFalseTotal
/\R\z/ ? : 0 0 0
ref $_[0] ? : 0 0 0
264 0 return $io;
265 }
266  
267  
268 ##==============================================================================
269 ## Footer
270 1 1;
271  
272 __END__
« TT/Persistent.pm ↑ Summary TT/Packed/at.pm »