blib/lib/Text/FixedLengthMultiline.pm

filter: Statementstmt 94.8% 202 / 213 Branchbran 84.9% 107 / 126 Conditioncond 80.9% 34 / 42 MC/DCmcdc 64.0% 16 / 25 Subroutinesub 95.2% 20 / 21 Podpod 100% 5 / 5 totaltotal 88.8% 384 / 432 CC 79 SCAR 24.6
CC
79
Cov
90%
parse_line
26.3
_build_column_re
14.4
_build_first_line_re
8.0
CRAP 11.7
↑ Summary
1 package Text::FixedLengthMultiline;
2  
3 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 our $VERSION = '0.071';
11 }
12  
13 4 ▶use constant FIRST => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use constant LAST => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use constant ANY => 3; # FIRST | LAST
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 my %continue_styles = (
18 'first' => FIRST,
19 'last' => LAST,
20 'any' => ANY
21 );
22  
23  
24 =encoding utf8
25  
26 =head1 NAME
27  
28 Text::FixedLengthMultiline - Parse text data formatted in space separated columns optionnaly on multiple lines
29  
30 =head1 SYNOPSIS
31  
32 use Text::FixedLengthMultiline;
33  
34 #234567890 12345678901234567890 12
35 my $text = <<EOT;
36 Alice Pretty girl!
37 Bob Good old uncle Bob,
38 very old. 92
39 Charlie Best known as Waldo 14
40 or Wally. Where's
41 he?
42 EOT
43  
44 my $fmt = Text::FixedLengthMultiline->new(format => ['!name' => 10, 1, 'comment~' => 20, 1, 'age' => -2 ]);
45  
46 # Compute the RegExp that matches the first line
47 my $first_line_re = $fmt->get_first_line_re();
48 # Compute the RegExp that matches a continuation line
49 my $continue_line_re = $fmt->get_continue_line_re();
50  
51 my @data;
52 my $err;
53 while ($text =~ /^([^\n]+)$/gm) {
54 my $line = $1;
55 push @data, {} if $line =~ $first_line_re;
56 if (($err = $fmt->parse_line($line, $data[$#data])) > 0) {
57 warn "Parse error at column $err";
58 }
59 }
60  
61 =head1 DESCRIPTION
62  
63 A row of data can be splitted on multiple lines of text with cell content
64 flowing in the same column space.
65  
66 =head1 FORMAT SPECIFICATION
67  
68 The format is given at the contruction time as an array ref. Modifying the
69 array content after the construction call is done at your own risks.
70  
71 The array contains the ordered sequence of columns. Each colmun can either be:
72  
73 =over
74  
75 =item *
76  
77 a positive integer representing the size of a separating column which is
78 expected to always be filled with spaces.
79  
80 =item *
81  
82 a string that matches this regexp: /^(?#mandatory)!?(?#name)[:alnum:]\w*(?:(?#multi)~(?#cont).?)?$/
83  
84 =over
85  
86 =item *
87  
88 C<!> means the column is mandatory
89  
90 =item *
91  
92 C<name> is the column name. This will be the key for the hash after parsing.
93  
94 =item *
95  
96 C<~> means the column data can be on multiple lines.
97  
98 =back
99  
100 =back
101  
102 =head1 METHODS
103  
104 =head2 new()
105  
106 Arguments:
107  
108 =over
109  
110 =item *
111  
112 C<format>: an array reference following the L<FORMAT SPECIFICATION>.
113  
114 =item *
115  
116 C<debug>
117  
118 =back
119  
120 Example:
121  
122 my $format = Text::FixedLengthMultiline->new(format => [ 2, col1 => 4, 1, '!col2' => 4 ]);
123  
124 =cut
125  
126 # TODO add 'continue-style': first/last/any
127 sub new
128 41 ▶{
Subroutine new: called CC=7 SCAR=19.6
Pod documented
129 41 my $class = shift;
130 41 my %params = @_;
131 41 ▶ (%params && exists $params{'format'}) or croak('['.__PACKAGE__."] Missing format");
Branch: 1 branch
BranchTrueFalseTotal
%params and exists $params{"format"} or croak("[Text::FixedLengthMultiline] Missing format") 40 1 41
Condition66%2/3
%params and exists $params{"format"}
!ll&&!rl&&r
1040
MC/DC50%1/2
%params and exists $params{"format"}
%params exists $params{"format"}
Truth table66%2/3
A and B
A= %paramsB= exists $params{"format"}
ABresult
0X0
100
111
132 40 ▶ ref $params{'format'} eq 'ARRAY' or croak('['.__PACKAGE__."] Invalid format: array ref expected");
Branch: 1 branch
BranchTrueFalseTotal
ref $params{"format"} eq "ARRAY" or croak("[Text::FixedLengthMultiline] Invalid format: array ref expected") 37 3 40
133 37 my $continue_style = ANY;
134 37 ▶ if (exists $params{'continue_style'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"continue_style"}) 23 14 37
135 23 my $style = $params{'continue_style'};
136 23 ▶ croak('['.__PACKAGE__."] Invalid continue_style: first/last/any expected") unless exists $continue_styles{$style};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $continue_styles{$style} 4 19 23
137 19 $continue_style = $continue_styles{$style};
138 }
139 # TODO Check the format, and report errors
140 my $self = {
141 FORMAT => $params{'format'},
142 # Maybe doing a copy would be a good idea...
143 # But we trust the user even if we all know
144 # he's a crazy programmer
145 33 ▶ DEBUG => exists $params{'debug'} && $params{'debug'},
Condition33%1/3
exists $params{'debug'} && $params{'debug'}
!ll&&!rl&&r
3300
MC/DC0%0/2
exists $params{'debug'} && $params{'debug'}
exists $params{'debug'} $params{'debug'}
Truth table33%1/3
A && B
A= exists $params{'debug'}B= $params{'debug'}
ABresult
0X0
100
111
146 CONTINUE_STYLE => $continue_style
147 };
148 33 bless $self, $class;
149 33 return $self;
150 }
151  
152  
153 =head2 C<parse_table($text)>
154  
155 Parse a table.
156  
157 my @table = $fmt->parse_table($text);
158  
159 Returns an array of hashes. Each hash is a row of data.
160  
161 =cut
162  
163 sub parse_table
164 1 ▶{
Subroutine parse_table: called CC=4 SCAR=13.9
Pod documented
165 1 my ($self, $text) = @_;
166 1 my $first_re = $self->get_first_line_re();
167 1 my @table;
168 my $err;
169 1 my $linenum = 1;
170 1 (pos $text) = 0;
171 1 ▶ while ($text =~ /^([^\n]+)$/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^([^\n]+)$/gm) 6 1 7
172 6 my $line = $1;
173 6 ▶ push @table, {} if $line =~ $first_re;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$first_re/ 3 3 6
174 6 ▶ if (($err = $self->parse_line($line, $table[$#table])) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($err = $self->parse_line($line, $table[$#table])) > 0) 0 6 6
175 0 croak "Parse error at line $linenum, column $err";
176 }
177 }
178 1 return @table;
179 }
180  
181  
182  
183  
184 =head2 C<parse_line($line, $hashref)>
185  
186 Parse a line of text and add parsed data to the hash.
187  
188 my $error = $fmt->parse_line($line, \%row_data);
189  
190 Multiple calls to C<parse_line()> with the same hashref may be needed to fully
191 read a "logical line" in case some columns are multiline.
192  
193 Returns:
194  
195 =over
196  
197 =item *
198  
199 C<-col>: Parse error. The value is a negative integer indicating the
200 character position in the line where the parse error occured.
201  
202 =item *
203  
204 C<0>: OK
205  
206 =item *
207  
208 C<col>: Missing data: need to feed next line to fill remining columns.
209 The value is the character position of the column where data is expected.
210  
211 =back
212  
213 =cut
214  
215 # TODO: return a RE in case of missing data
216 sub parse_line
217 40 ▶{
Subroutine parse_line: called CC=25 SCAR=32.7
Pod documented
218 40 my ($self, $line, $data) = @_;
219 40 my @fmt = @{$self->{FORMAT}};
220 40 my $col = 1;
221 40 my $ret = 0;
222 40 ▶ $line = '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 3 37 40
223 40 ▶ while ($#fmt >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#fmt >= 0) 86 22 108
224 86 my $f = shift @fmt;
225 86 my $data_len;
226 86 ▶ if ($f =~ /^\d+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f =~ /^\d+$/) { } 36 50 86
elsif ($f =~ /^(!?)([A-Za-z_]\w*)(?:(~)(.?))?$/) { } 50 0 50
227 # Spaces to skip
228 36 ▶ next if $f == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $f == 0 0 36 36
229 36 $line =~ /^( {0,$f})/;
230 36 $data_len = length $1;
231 36 ▶ return -($col+$data_len) if $data_len < $f;
Branch: 1 branch
BranchTrueFalseTotal
if $data_len < $f 14 22 36
232 } elsif ($f =~ /^(!?)([A-Za-z_]\w*)(?:(~)(.?))?$/) {
233 50 my ($mandatory, $field, $multi, $cont) = ($1, $2, $3, $4);
234 50 ▶ $multi = 0 unless defined $multi;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $multi 38 12 50
235 50 ▶ $cont = ' ' unless defined $cont && $cont ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cont and $cont ne "" 50 0 50
Condition66%2/3
defined $cont and $cont ne ""
!ll&&!rl&&r
38120
MC/DC0%0/2
defined $cont and $cont ne ""
defined $cont $cont ne ""
Truth table66%2/3
A and B
A= defined $contB= $cont ne ""
ABresult
0X0
100
111
236 50 my $len = shift @fmt;
237 50 ▶ next if $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 0 50 50
238 50 my $d = substr($line, 0, abs $len);
239 50 $data_len = length $d;
240 50 ▶ if ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) { } 38 12 50
241 38 $d =~ s/ +$//;
242 } else {
243 12 $d .= ' ' x -($data_len+$len);
244 12 $d =~ s/^ +//;
245 }
246 50 ▶ if ($d ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d ne "") 37 13 50
247 37 ▶ return -$col if !$multi && exists $data->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if not $multi and exists $data->{$field} 3 34 37
Condition100%3/3
not $multi and exists $data->{$field}
!ll&&!rl&&r
12223
MC/DC100%2/2
not $multi and exists $data->{$field}
not $multi exists $data->{$field}
Truth table100%3/3
not(A) and B
A= not $multiB= exists $data->{$field}
ABresult
0X0
100
111
248 34 ▶ if ($multi && exists $data->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi and exists $data->{$field}) { } 6 28 34
Condition100%3/3
$multi and exists $data->{$field}
!ll&&!rl&&r
2266
MC/DC100%2/2
$multi and exists $data->{$field}
$multi exists $data->{$field}
Truth table100%3/3
A and B
A= $multiB= exists $data->{$field}
ABresult
0X0
100
111
249 # Multilines => concat
250 6 $data->{$field} .= "\n" . $d;
251 6 ▶ $ret = $col if $ret == 0 && $d =~ /\Q$cont\E$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ret == 0 and $d =~ /\Q$cont\E$/ 0 6 6
Condition33%1/3
$ret == 0 and $d =~ /\Q$cont\E$/
!ll&&!rl&&r
060
MC/DC0%0/2
$ret == 0 and $d =~ /\Q$cont\E$/
$ret == 0 $d =~ /\Q$cont\E$/
Truth table33%1/3
A and B
A= $ret == 0B= $d =~ /\Q$cont\E$/
ABresult
0X0
100
111
252 } else {
253 28 $data->{$field} = $d;
254 }
255 }
256 47 ▶ $ret = $col if $mandatory && !exists $data->{$field} && $ret == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mandatory and not exists $data->{$field} and $ret == 0 1 46 47
Condition83%5/6
$mandatory and not exists $data->{$field} and $ret == 0
!ll&&!rl&&r
4601
$mandatory and not exists $data->{$field}
!ll&&!rl&&r
30161
MC/DC66%2/3
$mandatory and not exists $data->{$field} and $ret == 0
$mandatory not exists $data->{$field} $ret == 0
Truth table75%3/4
A and not(B) and C
A= $mandatoryB= not exists $data->{$field}C= $ret == 0
ABCresult
0XX0
10X0
1100
1111
257 } else {
258 0 warn "Bad format!\n";
259 0 return -$col;
260 }
261 69 $col += $data_len;
262 69 $line = substr($line, $data_len);
263 69 ▶ last if $ret != 0 && $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $ret != 0 and $line eq "" 1 68 69
Condition66%2/3
$ret != 0 and $line eq ""
!ll&&!rl&&r
6801
MC/DC50%1/2
$ret != 0 and $line eq ""
$ret != 0 $line eq ""
Truth table66%2/3
A and B
A= $ret != 0B= $line eq ""
ABresult
0X0
100
111
264 }
265 23 ▶ return -$col unless $line =~ /^ *$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^ *$/ 3 20 23
266 20 return $ret;
267 }
268  
269  
270  
271  
272 sub _dump_line_re()
273 0 ▶{
Subroutine _dump_line_re: not called CC=2 SCAR=17.9
274 0 ▶ while ($#_ >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ >= 0) 0 0 0
275 0 print "> [" . (shift @_) ."]\n";
276 0 print ' [' . join('] :: [', @{ (shift @_) }) . "]\n";
277 }
278 }
279  
280 sub _serialize_line_re()
281 76 ▶{
Subroutine _serialize_line_re: called CC=5 SCAR=16.1
282 #&_dump_line_re(@_);
283 76 my $re = '';
284 76 ▶ while ($#_ > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > -1) 69 76 145
285 # Pop the alternatives for the end of the line
286 69 my @b = grep(!/^$/, @{ (pop @_) });
287 # TODO remove duplicates
288 69 ▶ push @b, $re if $re ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $re ne "" 21 48 69
289 69 ▶ if ($#b > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#b > 0) { } 23 46 69
elsif ($#b > -1) { } 42 4 46
290 23 $re = "(?:" . join('|', @b) . ")";
291 } elsif ($#b > -1) {
292 42 $re = $b[0];
293 } else {
294 4 $re = '';
295 }
296 # Pop
297 69 $re = (pop @_) . $re;
298 #print "$re\n";
299 }
300 76 return $re;
301 }
302  
303 sub _parse_column_format($;$)
304 104 ▶{
Subroutine _parse_column_format: called CC=4 SCAR=14.0
305 104 my ($format, $width) = @_;
306 104 ▶ if ($format =~ /^(!?)([A-Za-z_]\w*)(?:(~)(.?))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ /^(!?)([A-Za-z_]\w*)(?:(~)(.?))?$/) { } 104 0 104
307 104 my %def = (
308 mandatory => $1,
309 name => $2,
310 multi => $3,
311 cont => $4,
312 width => abs $width
313 );
314 104 ▶ $def{multi} = '' unless defined $def{multi};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def{"multi"} 63 41 104
315 104 ▶ $def{align} = $width > 0 ? 'L' : 'R';
Branch: 1 branch
BranchTrueFalseTotal
$width > 0 ? : 85 19 104
316 104 return %def;
317 } else {
318 0 return undef;
319 }
320 }
321  
322 sub _build_repetition_re($;$;$)
323 101 ▶{
Subroutine _build_repetition_re: called CC=5 SCAR=17.5
324 101 my ($c, $min, $max) = @_;
325 101 ▶ return '' if $max <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max <= 0 0 101 101
326 101 ▶ if ($max == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max == 1) { } 0 101 101
327 0 ▶ $c .= '?' if $min <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $min <= 0 0 0 0
328 } else {
329 101 ▶ if ($min < $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < $max) { } 45 56 101
330 45 $c .= "{$min,$max}";
331 } else {
332 56 $c .= "{$max}";
333 }
334 }
335 101 return $c;
336 }
337  
338 sub _build_column_re
339 78 ▶{
Subroutine _build_column_re: called CC=14 SCAR=26.7
340 78 my $self = shift;
341 78 my %def = @_;
342 78 ▶ my $branch_multi = $def{multi} && exists $def{branch_multi} && $def{branch_multi};
Condition100%6/6
$def{'multi'} && exists $def{'branch_multi'} && $def{'branch_multi'}
!ll&&!rl&&r
61125
$def{"multi"} and exists $def{"branch_multi"}
!ll&&!rl&&r
372417
MC/DC100%3/3
$def{'multi'} && exists $def{'branch_multi'} && $def{'branch_multi'}
$def{"multi"} exists $def{"branch_multi"} $def{'branch_multi'}
Truth table100%4/4
A and B && C
A= $def{"multi"}B= exists $def{"branch_multi"}C= $def{'branch_multi'}
ABCresult
0XX0
10X0
1100
1111
343 78 ▶ my $re_label = $self->{DEBUG} ? "(?#_$def{mandatory}$def{name}$def{multi}_)" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'DEBUG'} ? : 0 78 78
344 78 ▶ my $re_spaces = $def{spaces} > 0 ? ' '.($def{spaces} > 1 ? "{$def{spaces}}":'') : '';
Branch: 2 branches
BranchTrueFalseTotal
$def{'spaces'} > 0 ? : 77 1 78
$def{'spaces'} > 1 ? : 68 9 77
345 78 my $width = $def{width};
346 78 my ($re_col_mand, $re_col_end, $re_col);
347 78 ▶ if ($def{mandatory} || $branch_multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def{'mandatory'} or $branch_multi) { } 56 22 78
Condition100%3/3
$def{"mandatory"} or $branch_multi
l!l&&r!l&&!r
54222
MC/DC100%2/2
$def{"mandatory"} or $branch_multi
$def{"mandatory"} $branch_multi
Truth table100%3/3
A or B
A= $def{"mandatory"}B= $branch_multi
ABresult
1X1
011
000
348 56 $re_col_mand = $re_spaces . $re_label;
349 56 ▶ if ($def{align} eq 'L') { # Left aligned
Branch: 1 branch
BranchTrueFalseTotal
if ($def{'align'} eq 'L') { } 45 11 56
350 45 $re_col_end = &_build_repetition_re('.', 0, $width-1);
351 45 ▶ unless ($branch_multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch_multi) { } 5 40 45
352 40 $re_col_mand .= '\S';
353 40 $re_col = &_build_repetition_re('.', $width-1, $width-1);
354 } else {
355 5 $re_col = &_build_repetition_re('.', $width, $width);
356 5 $re_col_end = '\S' . $re_col_end;
357 }
358 } else {
359 11 $re_col_mand .= &_build_repetition_re('.', $width-1, $width-1);
360 11 ▶ unless ($branch_multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch_multi) { } 0 11 11
361 11 $re_col_end = $re_col = '';
362 11 $re_col_mand .= '\S';
363 } else {
364 0 $re_col_end = '\S';
365 0 $re_col = '.';
366 }
367 }
368 } else {
369 22 $re_col_mand = '';
370 22 ▶ $re_col_end = $re_spaces . $re_label . '.' . ($width > 1 ? "{0,$width}" : '?');
Branch: 1 branch
BranchTrueFalseTotal
$width > 1 ? : 22 0 22
371 22 ▶ $re_col_end = "(?:$re_col_end)?" if $def{spaces};
Branch: 1 branch
BranchTrueFalseTotal
if $def{"spaces"} 22 0 22
372 22 ▶ $re_col = $re_spaces . $re_label . '.' . ($width > 1 ? "{$width}" : '' );
Branch: 1 branch
BranchTrueFalseTotal
$width > 1 ? : 22 0 22
373 }
374 #print "$def{name} => /$re_col_mand/ /$re_col_end/ /$re_col/ (spaces = $def{spaces})\n";
375 78 return ($re_col_mand, $re_col_end, $re_col);
376 }
377  
378 sub _has_multi(@)
379 17 ▶{
Subroutine _has_multi: called CC=3 SCAR=11.0
380 17 foreach (@_) {
381 43 ▶ return 1 if /!?[_[:alpha:]]\w+~/;
Branch: 1 branch
BranchTrueFalseTotal
if /!?[_[:alpha:]]\w+~/ 5 38 43
382 }
383 12 return 0;
384 }
385  
386  
387 # @_ is the format
388 # TODO handle the case where all columns are optionnal
389 # The RE is then the union of the cases where one of the colmuns, up to the first multi, is mandatory
390 sub _build_first_line_re
391 37 ▶{
Subroutine _build_first_line_re: called CC=8 SCAR=20.8
392 37 my $self = shift;
393 37 my $branch_multi = shift;
394 37 my $spaces = 0;
395 37 my @re = ();
396 37 my $re_acc = ''; # Accumulator
397 37 my $multi = '~'; # Force the initialisation of @re
398 37 ▶ while ($#_ >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ >= 0) 121 37 158
399 121 my $f = shift;
400 121 ▶ if ($f =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /^\d+$/) { } 60 61 121
401 60 $spaces += $f;
402 } else {
403 61 my %def = &_parse_column_format($f, shift);
404 61 ▶ if ($multi && ($branch_multi || $#re == -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi and $branch_multi || $#re == -1) 42 19 61
Condition100%6/6
$multi and $branch_multi || $#re == -1
!ll&&!rl&&r
14542
$branch_multi or $#re == -1
l!l&&r!l&&!r
3755
MC/DC100%3/3
$multi and $branch_multi || $#re == -1
$multi $branch_multi $#re == -1
Truth table100%4/4
A and B or C
A= $multiB= $branch_multiC= $#re == -1
ABCresult
0XX0
1000
11X1
1011
405 # The previous column was a multi. The following fields may not be
406 # on this line but on one of the next ones.
407 # So the end of the line is optionnal.
408 # We are starting a new altenative in the RE.
409 42 push @re, $re_acc, [ ];
410 42 $re_acc = '';
411 }
412 61 my ($re_col_mand, $re_col_end, $re_col) = $self->_build_column_re(%def, spaces => $spaces);
413 61 ▶ if ($def{mandatory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def{'mandatory'}) { } 40 21 61
414 # Flush optional columns and append this column
415 40 $re[$#re-1] .= $re_acc . $re_col_mand;
416 40 ▶ if ($re_col_end eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($re_col_end eq '') { } 8 32 40
417 8 $re[$#re] = [ ];
418 } else {
419 32 $re[$#re] = [ $re_col_end ];
420 }
421 40 $re_acc = $re_col;
422 } else {
423 # Save column format for later
424 21 push @{$re[$#re]}, $re_acc . $re_col_mand . $re_col_end;
425 21 $re_acc .= $re_col_mand . $re_col;
426 }
427 61 $spaces = 0;
428 61 $multi = $def{multi};
429 }
430 }
431 37 return @re;
432 }
433  
434 sub _build_continue_line_re
435 39 ▶{
Subroutine _build_continue_line_re: called CC=7 SCAR=19.5
436 39 my $self = shift;
437 39 my $spaces = 0;
438 39 my $multi = '~'; # Force the initialisation of @re
439 39 ▶ while ($#_ >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ >= 0) 85 22 107
440 85 my $f = shift;
441 85 ▶ if ($f =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /^\d+$/) { } 42 43 85
442 42 $spaces += $f;
443 } else {
444 43 my %def = &_parse_column_format($f, shift);
445 43 ▶ unless ($def{multi}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($def{"multi"}) 26 17 43
446 26 $spaces += $def{width};
447 26 next;
448 }
449 17 my @re;
450 17 my ($re_col_end, $re_col);
451 17 ($re[0], $re_col_end, $re_col) = $self->_build_column_re(%def, spaces => $spaces, branch_multi => &_has_multi(@_));
452 17 push @re, [ $re_col_end ];
453 17 my @re_end;
454 17 ▶ push @re_end, &_serialize_line_re($self->_build_continue_line_re(@_)) if $self->{CONTINUE_STYLE} & FIRST;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"CONTINUE_STYLE"} & 1 13 4 17
455 17 ▶ push @re_end, &_serialize_line_re($self->_build_first_line_re(1, @_)) if $self->{CONTINUE_STYLE} & LAST;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"CONTINUE_STYLE"} & 2 11 6 17
456 17 @re_end = grep !/^$/, @re_end;
457 #pop @re_end if $#re_end == 1 && $re_end[1] eq $re_end[0];
458 17 ▶ push @re, $re_col, [ @re_end ] if (@re_end);
Branch: 1 branch
BranchTrueFalseTotal
if @re_end 10 7 17
459 17 return @re;
460 }
461 }
462 22 return ();
463 }
464  
465 =head2 C<get_first_line_re()>
466  
467 Returns a regular expression that matches the first line of a "logical line"
468 of data.
469  
470 my $re = $fmt->get_first_line_re();
471  
472 =cut
473  
474 sub get_first_line_re
475 27 ▶{
Subroutine get_first_line_re: called CC=4 SCAR=13.9
Pod documented
476 27 my $self = shift;
477 27 ▶ if (!exists $self->{FIRST_LINE_RE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"FIRST_LINE_RE"}) 26 1 27
478 26 my @re;
479 26 ▶ if ($self->{CONTINUE_STYLE} == FIRST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'CONTINUE_STYLE'} == 1) { } 6 20 26
480 6 @re = $self->_build_first_line_re(0, @{$self->{FORMAT}});
481 } else {
482 20 @re = $self->_build_first_line_re(1, @{$self->{FORMAT}});
483 }
484 26 my $re = &_serialize_line_re(@re);
485 26 ▶ $self->{FIRST_LINE_RE} = ($re eq '' ? undef : qr/^$re *$/);
Branch: 1 branch
BranchTrueFalseTotal
$re eq '' ? : 3 23 26
486 }
487 27 return $self->{FIRST_LINE_RE};
488 }
489  
490 =head2 C<get_continue_line_re()>
491  
492 Returns a regular expression that matches the 2nd line and the following
493 lines of a "logical line".
494  
495 my $re = $fmt->get_continue_line_re();
496  
497 Returns undef if the format specification does not contains any column that
498 can be splitted on multiples lines.
499  
500 =cut
501  
502 # continue-style: first (only cont columns can appear on a continue line)
503 sub get_continue_line_re
504 26 ▶{
Subroutine get_continue_line_re: called CC=3 SCAR=11.0
Pod documented
505 26 my $self = shift;
506 26 ▶ if (!exists $self->{CONTINUE_LINE_RE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"CONTINUE_LINE_RE"}) 26 0 26
507 26 my @re = $self->_build_continue_line_re(@{$self->{FORMAT}});
508 #&_dump_line_re(@re);
509 26 my $re = &_serialize_line_re(@re);
510 26 ▶ $self->{CONTINUE_LINE_RE} = ($re eq '' ? undef : qr/^$re *$/);
Branch: 1 branch
BranchTrueFalseTotal
$re eq '' ? : 13 13 26
511 }
512 26 return $self->{CONTINUE_LINE_RE};
513 }
514  
515 4 1; # Magic for module end
516  
517 __END__
↑ Summary