blib/lib/Text/Filter/Cooked.pm

filter: Statementstmt 85.2% 81 / 95 Branchbran 60.0% 24 / 40 Conditioncond 58.8% 10 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 89.6% 26 / 29 Podpod 20.0% 3 / 15 totaltotal 71.5% 148 / 207 CC 28 SCAR 19.8
CC
28
Cov
76%
_diamond
30.0
readline
13.4
__ANON__
5.5
CRAP 7.3
« blib/lib/Text/Filter.pm ↑ Summary examples/testfilter.pl »
1 #! perl
2  
3 package Text::Filter::Cooked;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = "0.02";
7 1 ▶use base q{Text::Filter};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # later use Encode;
11  
12 =head1 NAME
13  
14 Text::Filter::Cooked - Cooked reader for input files
15  
16 =head1 SYNOPSIS
17  
18 use Text::Filter::Cooked;
19 my $f = Text::Filter::Cooked->new
20 (input => 'myfile.dat',
21 comment => "#",
22 join_lines => "\\");
23  
24 while ( my $line = $f->readline ) {
25 printf("%3d\t%s\n", $f->lineno, $line);
26 }
27  
28 =head1 DESCRIPTION
29  
30 Text::Filter::Cooked is a generic input reader. It takes care of a
31 number of things that are commonly used when reading data and
32 configuration files.
33  
34 =over 4
35  
36 =item *
37  
38 Excess whitespace (leading and trailing) may be removed automatically.
39 Also, multiple whitespace characters may be replaced by a single blank.
40  
41 =item *
42  
43 Empty lines may be ignored automatically.
44  
45 =item *
46  
47 Lines that end with a custom defined join symbol, ususally a
48 backslash, are joined with the next line.
49  
50 =item *
51  
52 Lines that start with a custom defined comment symbol are ignored.
53  
54 =back
55  
56 =for later
57 On top of this, if the input file starts with a Unicode BOM, the input
58 will be correctly decoded into Perl internal format. It is also
59 possible to change the encoding used in a single file as often as
60 desired. See L<INPUT ENCODING>.
61  
62 Text::Filter::Cooked is based on Text::Filter, see L<Text::Filter>.
63  
64 =cut
65  
66 ################ Attribute Controls ################
67  
68 1 my %_attributes =
69 ( ignore_empty_lines => 1,
70 ignore_leading_whitespace => 1,
71 ignore_trailing_whitespace => 1,
72 compress_whitespace => 1,
73 # later input => \&_diamond,
74 # later input_encoding => undef,
75 input_postread => 'chomp',
76 output_prewrite => 'newline',
77 comment => undef,
78 join_lines => undef,
79 _lineno => undef,
80 _open => 0,
81 );
82  
83 3 ▶sub _standard_atts {
Subroutine _standard_atts: called CC=1 SCAR=0.0
84 3 my $self = shift;
85 3 my %k;
86 3 @k{ $self->SUPER::_standard_atts, keys %_attributes } = (0);
87 3 return keys %k;
88 }
89  
90 27 ▶sub _attr_default {
Subroutine _attr_default: called CC=2 SCAR=6.9
91 27 my ($self, $attr) = @_;
92 27 ▶ return $_attributes{$attr} if exists $_attributes{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_attributes{$attr} 24 3 27
93 3 return $self->SUPER::_attr_default($attr);
94 }
95  
96 ################ Constructor ################
97  
98 =head1 CONSTRUCTOR
99  
100 The constructor is called new() and takes a hash with attributes as
101 its parameter.
102  
103 The following attributes are recognized and used by the constructor,
104 all others are passed to the base class, Text::Filter.
105  
106 =over 4
107  
108 =item ignore_empty_lines
109  
110 If true, empty lines encountered in the input are ignored.
111  
112 =item ignore_leading_whitespace
113  
114 If true, leading whitespace encountered in the input is ignored.
115  
116 =item ignore_trailing_whitespace
117  
118 If true, trailing whitespace encountered in the input is ignored.
119  
120 =item compress_whitespace
121  
122 If true, multiple adjacent whitespace are compressed to a single space.
123  
124 =item join_lines
125  
126 This must be set to a string. Input lines that end with this string
127 (not taking the final line ending into account) are joined with the
128 next line read from the input.
129  
130 =item comment
131  
132 This must be set to a string. Input lines that start with this string
133 are ignored.
134  
135 =for later
136 (but see L<INPUT ENCODING>).
137  
138 =begin later item input_encoding
139  
140 Assume the input file to have this encoding.
141  
142 Setting input_encoding will enable automatic and transparant handling
143 of different file encodings, see L<INPUT ENCODING>.
144  
145 =back
146  
147 =cut
148  
149 # Inherited from base class.
150  
151 ################ Attributes ################
152  
153 =head1 METHODS
154  
155 All attributes have set and get methods, e.g., C<set_comment> and
156 C<get_input_encoding>.
157  
158 Other methods:
159  
160 =over 4
161  
162 =item readline
163  
164 Read a single line of input. If line ignoring is in effect, the
165 operation will be repeated internally until there is data to return.
166  
167 =item lineno
168  
169 Returns the number of the last line that was read from the input.
170  
171 =item is_eof
172  
173 Returns true iff the last record from the input has been read.
174  
175 =back
176  
177 =cut
178  
179 3 ▶sub set_input {
Subroutine set_input: called CC=2 SCAR=7.6
Pod documented
180 3 my ($self, $input) = @_;
181 0 ▶ $input = sub { $self->_diamond } if $input eq \&_diamond;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $input eq \&Text::Filter::Cooked::_diamond 0 3 3
182 3 $self->SUPER::set_input($input);
183 }
184  
185 3 ▶sub set_ignore_empty_lines {
Subroutine set_ignore_empty_lines: called CC=1 SCAR=0.0
Pod undocumented
186 3 $_[0]->{ignore_empty_lines} = $_[1];
187 3 return;
188 }
189  
190 30 ▶sub get_ignore_empty_lines {
Subroutine get_ignore_empty_lines: called CC=1 SCAR=0.0
Pod undocumented
191 30 return $_[0]->{ignore_empty_lines};
192 }
193  
194 =begin later
195  
196 sub set_input_encoding {
197 my ($self, $enc) = @_;
198 $self->{input_encoding} = $enc;
199 if ( my $fd = $self->get_filter_input_fd ) {
200 binmode($fd, ':raw');
201 }
202 # warn("Input encoding = $enc\n");
203 return;
204 }
205  
206 sub get_input_encoding {
207 return $_[0]->{input_encoding};
208 }
209  
210 =cut
211  
212 3 ▶sub set_ignore_trailing_whitespace {
Subroutine set_ignore_trailing_whitespace: called CC=1 SCAR=0.0
Pod undocumented
213 3 $_[0]->{ignore_trailing_whitespace} = $_[1];
214 3 return;
215 }
216  
217 9 ▶sub get_ignore_trailing_whitespace {
Subroutine get_ignore_trailing_whitespace: called CC=1 SCAR=0.0
Pod undocumented
218 9 return $_[0]->{ignore_trailing_whitespace};
219 }
220  
221 33 ▶sub _set_lineno {
Subroutine _set_lineno: called CC=2 SCAR=6.9
222 33 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 30 3 33
223 30 $_[0]->{_lineno}++
224 }
225 else {
226 3 $_[0]->{_lineno} = $_[1];
227 }
228 33 return;
229 }
230  
231 18 ▶sub _get_lineno {
Subroutine _get_lineno: called CC=1 SCAR=0.0
232 18 return $_[0]->{_lineno};
233 }
234  
235 3 ▶sub set_comment {
Subroutine set_comment: called CC=3 SCAR=12.0
Pod undocumented
236 3 my ($self, $c) = @_;
237 # This check will probably fail with a custom regexp engine.
238 3 ▶ $c = qr/^\Q$c\E(.*)$/ unless !defined($c) || ref($c) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $c or ref $c eq "Regexp" 3 0 3
Condition33%1/3
not defined $c or ref $c eq "Regexp"
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $c or ref $c eq "Regexp"
not defined $c ref $c eq "Regexp"
Truth table33%1/3
not(A) or B
A= not defined $cB= ref $c eq "Regexp"
ABresult
1X1
011
000
239 3 $self->{comment} = $c;
240 3 return;
241 }
242  
243 24 ▶sub get_comment {
Subroutine get_comment: called CC=1 SCAR=0.0
Pod undocumented
244 24 return $_[0]->{comment};
245 }
246  
247 3 ▶sub set_ignore_leading_whitespace {
Subroutine set_ignore_leading_whitespace: called CC=1 SCAR=0.0
Pod undocumented
248 3 $_[0]->{ignore_leading_whitespace} = $_[1];
249 3 return;
250 }
251  
252 9 ▶sub get_ignore_leading_whitespace {
Subroutine get_ignore_leading_whitespace: called CC=1 SCAR=0.0
Pod undocumented
253 9 return $_[0]->{ignore_leading_whitespace};
254 }
255  
256 3 ▶sub set_compress_whitespace {
Subroutine set_compress_whitespace: called CC=1 SCAR=0.0
Pod undocumented
257 3 $_[0]->{compress_whitespace} = $_[1];
258 3 return;
259 }
260  
261 9 ▶sub get_compress_whitespace {
Subroutine get_compress_whitespace: called CC=1 SCAR=0.0
Pod undocumented
262 9 return $_[0]->{compress_whitespace};
263 }
264  
265 3 ▶sub set_join_lines {
Subroutine set_join_lines: called CC=3 SCAR=12.0
Pod undocumented
266 3 my ($self, $v) = @_;
267 # This check will probably fail with a custom regexp engine.
268 3 ▶ $v = qr/^(.*)\Q$v\E$/ unless !defined($v) || ref($v) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $v or ref $v eq "Regexp" 3 0 3
Condition33%1/3
not defined $v or ref $v eq "Regexp"
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $v or ref $v eq "Regexp"
not defined $v ref $v eq "Regexp"
Truth table33%1/3
not(A) or B
A= not defined $vB= ref $v eq "Regexp"
ABresult
1X1
011
000
269 3 $self->{join_lines} = $v;
270 3 return;
271 }
272  
273 21 ▶sub get_join_lines {
Subroutine get_join_lines: called CC=1 SCAR=0.0
Pod undocumented
274 21 return $_[0]->{join_lines};
275 }
276  
277 3 ▶sub _set_eof {
Subroutine _set_eof: called CC=1 SCAR=0.0
278 3 $_[0]->{_eof} = 1;
279 3 return;
280 }
281  
282 12 ▶sub _is_eof {
Subroutine _is_eof: called CC=1 SCAR=0.0
283 12 return $_[0]->{_eof};
284 }
285  
286 3 ▶sub _set_open {
Subroutine _set_open: called CC=1 SCAR=0.0
287 3 $_[0]->{_open} = 1;
288 3 return;
289 }
290  
291 0 ▶sub _is_open {
Subroutine _is_open: not called CC=1 SCAR=6.9
292 0 return $_[0]->{_open};
293 }
294  
295 ################ Methods ################
296  
297 12 ▶sub readline {
Subroutine readline: called CC=13 SCAR=26.0
Pod documented
298 12 my $self = shift;
299  
300 12 ▶ return if $self->_is_eof;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_eof 0 12 12
301  
302 9 ▶ my $post = sub {
Subroutine __ANON__: called CC=5 SCAR=17.1
303 9 for ( shift ) {
304  
305 # Whitespace ignore + compress.
306 9 ▶ s/^\s+// if $self->get_ignore_leading_whitespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_ignore_leading_whitespace 9 0 9
307 9 ▶ s/\s+$// if $self->get_ignore_trailing_whitespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_ignore_trailing_whitespace 9 0 9
308 9 ▶ s/\s+/ /g if $self->get_compress_whitespace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_compress_whitespace 9 0 9
309  
310 9 return $_;
311 }
312 12 };
313  
314 12 my $line;
315 my $pre;
316  
317 12 ▶ while ( defined ($line = $self->SUPER::readline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $self->SUPER::readline)) 30 3 33
318  
319 =begin later
320  
321 my $ienc = $self->get_input_encoding;
322 if ( $ienc && ! defined $self->_get_lineno ) {
323 # Detecting BOM...
324 if ( substr($line, 0, 2) eq "\xff\xfe" ) {
325 # Found BOM (BE)
326 $line = substr($line, 2);
327 $self->set_input_encoding($ienc = "utf-16-be");
328 }
329 elsif ( substr($line, 0, 2) eq "\xfe\xff" ) {
330 # Found BOM (LE)
331 $line = substr($line, 2);
332 $self->set_input_encoding($ienc = "utf-16le");
333 }
334 }
335  
336 =cut
337  
338 30 $self->_set_lineno;
339 30 ▶ $self->{_start_line} = $self->_get_lineno unless defined $pre;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pre 18 12 30
340  
341 =begin later
342  
343 if ( $ienc ) {
344 $line = decode($ienc, $line, 0);
345 }
346  
347 =cut
348  
349 # Feature: ignore_empty_lines.
350 30 ▶ next unless $self->get_ignore_empty_lines && $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_ignore_empty_lines and $line =~ /\S/ 6 24 30
Condition66%2/3
$self->get_ignore_empty_lines and $line =~ /\S/
!ll&&!rl&&r
0624
MC/DC50%1/2
$self->get_ignore_empty_lines and $line =~ /\S/
$self->get_ignore_empty_lines $line =~ /\S/
Truth table66%2/3
A and B
A= $self->get_ignore_empty_linesB= $line =~ /\S/
ABresult
0X0
100
111
351  
352 24 my $t = $self->get_comment;
353 24 ▶ if ( $t && $line =~ $t ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t and $line =~ /$t/) 3 21 24
Condition66%2/3
$t and $line =~ /$t/
!ll&&!rl&&r
0213
MC/DC50%1/2
$t and $line =~ /$t/
$t $line =~ /$t/
Truth table66%2/3
A and B
A= $tB= $line =~ /$t/
ABresult
0X0
100
111
354  
355 =begin later
356 $line = $1;
357 if ( $line =~ /^\s*
358 content-type \s*
359 : \s*
360 text \s* (?: \/ \s* plain \s* )?
361 ; \s* charset \s* = \s*
362 ([^\s;]+)
363 \s* $
364 /mix ) {
365 $self->set_input_encoding($1);
366 }
367  
368 =cut
369  
370 3 next;
371 }
372  
373 21 $t = $self->get_join_lines;
374 21 ▶ if ( $t && $line =~ $t ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t and $line =~ /$t/) 12 9 21
Condition66%2/3
$t and $line =~ /$t/
!ll&&!rl&&r
0912
MC/DC50%1/2
$t and $line =~ /$t/
$t $line =~ /$t/
Truth table66%2/3
A and B
A= $tB= $line =~ /$t/
ABresult
0X0
100
111
375 12 ▶ $pre ||= "";
Condition100%2/2
$pre ||= ""
l!l
93
MC/DC100%1/1
$pre ||= ""
$pre
Truth table100%2/2
A
A= $pre
Aresult
11
00
376 12 $pre .= $1;
377 12 next;
378 }
379  
380 9 ▶ return $post->(defined $pre ? "$pre$line" : $line);
Branch: 1 branch
BranchTrueFalseTotal
defined $pre ? : 3 6 9
381 }
382 3 $self->_set_eof;
383  
384 =for later
385 $self->set_input_encoding($self->{input_encoding});
386  
387 =cut
388  
389 3 ▶ return $post->($pre) if defined $pre;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pre 0 3 3
390 3 return;
391 }
392  
393 3 ▶sub lineno {
Subroutine lineno: called CC=1 SCAR=0.0
Pod documented
394 3 my $self = shift;
395 3 return $self->{_start_line};
396 }
397  
398 0 ▶sub _diamond {
Subroutine _diamond: not called CC=5 SCAR=34.0
399 0 my $self = shift;
400  
401 0 while ( 1 ) {
402 0 ▶ unless ( $self->_is_open ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_open) 0 0 0
403 0 ▶ return unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 0 0
404 0 my $argv = shift(@ARGV);
405 0 $self->{_argf} = undef;
406 0 ▶ open($self->{_argf}, '< :raw', $argv)
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"_argf"}, "< :raw", $argv or die "${argv}: $!\n" 0 0 0
407 or die("$argv: $!\n");
408 0 $self->_set_open(1);
409 }
410 0 my $result = $self->{_argf}->readline;
411 0 ▶ return $result if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 0 0 0
412 0 close($self->{_argf});
413 0 $self->_set_open(0);
414 }
415 }
416  
417 1 1;
418  
419 __END__
« blib/lib/Text/Filter.pm ↑ Summary examples/testfilter.pl »