blib/lib/Text/NumericData.pm

filter: Statementstmt 85.2% 237 / 278 Branchbran 66.6% 116 / 174 Conditioncond 70.8% 17 / 24 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 88.2% 15 / 17 Podpod 46.6% 7 / 15 totaltotal 76.5% 401 / 524 SCAR 53.8
CC
102
Cov
78%
line_check
55.3
data_line
27.6
chomp_line
12.0
CRAP 216.9
« blib/lib/Text/NumericData/App/txdfromtcad.pm ↑ Summary blib/lib/Text/NumericData/App/txdextrema.pm »
1 package Text::NumericData;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # TODO: optimize those regexes, compile once in constructor
7  
8 # major.minor.bugfix, the latter two with 3 digits each
9 # It's not pretty, but I gave up on 1.2.3 style.
10 14 our $VERSION = '2.004000';
11 14 our $version = $VERSION;
12 14 $VERSION = eval $VERSION;
13  
14 14 our $years = '2005-2023';
15 14 our $copyright = 'Copyright (c) '.$years.' Thomas Orgis, Free Software licensed under the same terms as Perl 5.10';
16 14 our $author = 'Thomas Orgis <thomas@orgis.org>';
17  
18 # TODO: More smarts in separator search.
19 # One should find ', ' as separator in
20 # a / c, d /e
21 14 my $newhite = '[^\S\015\012]'; # whitespace that is no line end character
22 14 my $trenner = $newhite.'+|,'.$newhite.'*|;'.$newhite.'*';
23 14 my $ntrenner = '[^\s,;]+'; # also excludes CR LF
24 14 my $lend = '[\012\015]+';
25 14 my $nlend = '[^\012\015]';
26 14 my $quotematch = "['\"]";
27  
28 14 my %endname = ("\015\012"=>'DOS', "\012"=>'UNIX', "\015"=>'MAC');
29 14 my %endstring = reverse %endname;
30  
31 # Fallback defaults if anything else fails.
32 14 our $default_sep = "\t";
33 14 our $default_eol = $/;
34 14 our $default_comchar = '#';
35 14 our $default_quote = 1;
36 14 our $default_quotechar = '"';
37  
38 our %help =
39 (
40 separator=>'use this separator for input (otherwise deduce from data; TAB is another way to say "tabulator", fallback is'.$default_sep.')'
41 ,outsep=>'use this separator for output (leave undefined to use input separator, fallback to '.($default_sep eq "\t" ? 'TAB' : $default_sep).')'
42 14 ▶ ,lineend=>'line ending to use: ('.join(', ', sort keys %endstring).' or be explicit if you can, taken from data if undefined, finally resorting to '.(defined $endname{$default_eol} ? $endname{$default_eol} : $default_eol).')'
Branch: 2 branches
BranchTrueFalseTotal
$Text::NumericData::default_sep eq "\t" ? : 14 0 14
defined $endname{$Text::NumericData::default_eol} ? : 14 0 14
43 ,comchar=>'comment character (if not set, deduce from data or use '.$default_comchar.')'
44 ,numregex=>'regex for matching numbers'
45 ,numformat=>'printf formats to use (if there is no "%" present at all, one will be prepended)'
46 ,comregex=>'regex for matching comments'
47 ,quote=>'quote titles'
48 ,quotechar=>'quote character to use (derived from input or '.$default_quotechar.')'
49 ,strict=>'strictly split data lines at configured separator (otherwise more fuzzy logic is involved)'
50 ,text=>'allow text as data (not first column)'
51 ,fill=>'fill value for undefined data'
52 ,black=>'ignore whitespace at beginning and end of line (disables strict mode)'
53 ,empty=>'treat empty lines as empty data sets, preserving them in output'
54 );
55  
56 # These are defaults for user settings.
57 14 our %defaults =
58 (
59 'separator',undef,
60 'outsep', undef,
61 'lineend', undef,
62 'comchar', undef,
63 'numregex', '[\+\-]?\d*\.?\d*[eE]?\+?\-?\d*',
64 'numformat',[],
65 'comregex','[#%]*'.$newhite.'*',
66 'quote',undef,
67 'quotechar',undef,
68 'strict', 0,
69 'text', 1
70 ,'fill',undef # a value to fill in for non-existent but still demanded data
71 ,'black', 0
72 ,'empty',0
73 );
74  
75 sub new
76 {
77 60 ▶ my $class = shift;
Subroutine new: called CC=11 SCAR=24.0
Pod undocumented
78 60 my $self = {};
79 60 bless $self, $class;
80 # Only pick the parts of the config hash that are of interest here.
81 60 my $gotconf = shift;
82 60 $self->{gotconfig} = {};
83 60 for my $n (keys %defaults)
84 {
85 840 ▶ if(ref $gotconf->{$n})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $gotconf->{$n}) { } 58 782 840
86 {
87 58 $self->{gotconfig}{$n} = dclone($gotconf->{$n});
88 }
89 else
90 {
91 782 $self->{gotconfig}{$n} = $gotconf->{$n};
92 }
93 }
94 60 foreach my $n (@{$self->{gotconfig}->{numformat}})
95 {
96 41 ▶ $n = '%'.$n unless $n =~ /\%/;
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ /\%/ 37 4 41
97 }
98 # Expand named special characters for line ending.
99 60 ▶ if(defined $gotconf->{lineend})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gotconf->{"lineend"}) 37 23 60
100 {
101 $self->{gotconfig}{lineend} = defined $endstring{$gotconf->{lineend}}
102 ? $endstring{$gotconf->{lineend}}
103 37 ▶ : $gotconf->{lineend};
Branch: 1 branch
BranchTrueFalseTotal
defined $endstring{$gotconf->{'lineend'}} ? : 36 1 37
104 }
105 60 for (qw(separator outsep))
106 {
107 120 ▶ if(defined $gotconf->{$_} and $gotconf->{$_} eq 'TAB'){ $self->{gotconfig}{$_} = "\t"; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gotconf->{$_} and $gotconf->{$_} eq "TAB") 2 118 120
Condition100%3/3
defined $gotconf->{$_} and $gotconf->{$_} eq "TAB"
!ll&&!rl&&r
11262
MC/DC100%2/2
defined $gotconf->{$_} and $gotconf->{$_} eq "TAB"
defined $gotconf->{$_} $gotconf->{$_} eq "TAB"
Truth table100%3/3
A and B
A= defined $gotconf->{$_}B= $gotconf->{$_} eq "TAB"
ABresult
0X0
100
111
108 }
109  
110 $self->{gotconfig}{strict} = 0
111 60 ▶ if $self->{gotconfig}{black};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"gotconfig"}{"black"} 1 59 60
112 60 $self->init();
113 60 return $self;
114 }
115  
116 sub init
117 {
118 77 ▶ my $self = shift;
Subroutine init: called CC=5 SCAR=16.1
Pod undocumented
119 77 %{$self->{config}} = %{$self->{gotconfig}};
120 77 $self->{comments} = []; #some comment in header
121 77 $self->{guessquote} = undef;
122 77 $self->{titles} = []; #column titles
123 77 $self->{title} = undef; #file title
124 77 foreach my $k ('numregex','numformat','comregex','fill')
125 {
126 308 ▶ $self->{config}{$k} = $defaults{$k} unless defined $self->{config}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{$k} 81 227 308
127 }
128 # Strict mode needs some set separator.
129 77 ▶ if($self->{config}{strict} and not defined $self->{config}{separator})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"strict"} and not defined $self->{"config"}{"separator"}) 1 76 77
Condition100%3/3
$self->{"config"}{"strict"} and not defined $self->{"config"}{"separator"}
!ll&&!rl&&r
7511
MC/DC100%2/2
$self->{"config"}{"strict"} and not defined $self->{"config"}{"separator"}
$self->{"config"}{"strict"} not defined $self->{"config"}{"separator"}
Truth table100%3/3
A and not(B)
A= $self->{"config"}{"strict"}B= not defined $self->{"config"}{"separator"}
ABresult
0X0
100
111
130 {
131 1 $self->{config}{separator} = $default_sep;
132 }
133 }
134  
135 #line_check($line, $onlycheck)
136 #$onlycheck: 0/undef: do full search for file/line titles and line end, etc.
137 # 1: only determine if data or not
138 sub line_check #return 1 and set separator and line ending if data line and 0 otherwise
139 {
140 101 ▶ my $self = shift;
Subroutine line_check: called CC=40 SCAR=40.1
Pod documented
141 # temporary hack until fully switching to value instead of ref, which is a ref anyway
142 101 ▶ my $lr = ref $_[0] ? $_[0] : \$_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 30 71 101
143 101 my $oc = $_[1];
144 101 my $zahl = $self->{config}{numregex};
145 101 my $seppl = $trenner;
146 101 ▶ $seppl = $self->{config}{separator} if $self->{config}{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"strict"} 14 87 101
147 #the leading whitespace is a workaround for TISEAN
148 #good or bad? It should not break any files that worked before...
149 101 ▶ if( ${$lr} =~ /^\s*$/ and ${$lr} =~ /^($nlend*)($lend)$/o )
Branch: 1 branch
BranchTrueFalseTotal
if (${$lr;} =~ /^\s*$/ and ${$lr;} =~ /^($nlend*)($lend)$/o) 2 99 101
Condition66%2/3
${$lr;} =~ /^\s*$/ and ${$lr;} =~ /^($nlend*)($lend)$/o
!ll&&!rl&&r
9902
MC/DC50%1/2
${$lr;} =~ /^\s*$/ and ${$lr;} =~ /^($nlend*)($lend)$/o
${$lr;} =~ /^\s*$/ ${$lr;} =~ /^($nlend*)($lend)$/o
Truth table66%2/3
A and B
A= ${$lr;} =~ /^\s*$/B= ${$lr;} =~ /^($nlend*)($lend)$/o
ABresult
0X0
100
111
150 {
151 2 ▶ $self->{config}{lineend} = $2 unless defined $self->{config}{lineend};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"lineend"} 0 2 2
152 # An empty line counts as comment when it comes after a title.
153 2 push(@{$self->{comments}},$1)
154 2 ▶ if defined $self->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"title"} 2 0 2
155 2 return 0;
156 }
157 99 ▶ if(${$lr} =~ /^\s*($zahl)(($seppl)$nlend*|)($lend)$/)
Branch: 2 branches
BranchTrueFalseTotal
if (${$lr;} =~ /^\s*($zahl)(($seppl)$nlend*|)($lend)$/) { } 41 58 99
elsif ($oc) { } 0 58 58
158 {
159 41 my ($num, $end, $sep) = ($1, $4, $3);
160 41 my $piece = $1.$2;
161 41 ▶ unless(not defined $end or defined $self->{config}{lineend})
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $end or defined $self->{"config"}{"lineend"}) 1 40 41
Condition66%2/3
not defined $end or defined $self->{"config"}{"lineend"}
l!l&&r!l&&!r
0401
MC/DC50%1/2
not defined $end or defined $self->{"config"}{"lineend"}
not defined $end defined $self->{"config"}{"lineend"}
Truth table66%2/3
not(A) or B
A= not defined $endB= defined $self->{"config"}{"lineend"}
ABresult
1X1
011
000
162 {
163 1 $self->{config}{lineend} = $end;
164 }
165 41 ▶ unless($self->{config}{text})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"config"}{"text"}) 2 39 41
166 {
167 # If text is not allowed, we strictly only want
168 # numbers and separators and line end.
169 # Let's get expensive: Remove everything we know. if there is something
170 # left, we got text.
171 2 my $linecopy = ${$lr};
172 2 $linecopy =~ s/$seppl//g;
173 2 $linecopy =~ s/($zahl|\s+|$lend)//g;
174 2 ▶ if($linecopy ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($linecopy ne "") 1 1 2
175 {
176 1 ▶ if( defined $self->{title} ){ push(@{$self->{comments}},$piece); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'title'}) { } 1 0 1
177 0 else{ $self->{title} = $piece; }
178 1 return 0;
179 }
180 }
181 # sanity check for loosened definition of number... at least one digit shall be there
182 40 ▶ if($num =~ /\d/)
Branch: 1 branch
BranchTrueFalseTotal
if ($num =~ /\d/) 40 0 40
183 {
184 40 ▶ unless(not defined $sep or defined $self->{config}{separator})
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $sep or defined $self->{"config"}{"separator"}) 33 7 40
Condition100%3/3
not defined $sep or defined $self->{"config"}{"separator"}
l!l&&r!l&&!r
6133
MC/DC100%2/2
not defined $sep or defined $self->{"config"}{"separator"}
not defined $sep defined $self->{"config"}{"separator"}
Truth table100%3/3
not(A) or B
A= not defined $sepB= defined $self->{"config"}{"separator"}
ABresult
1X1
011
000
185 {
186 33 $self->{config}{separator} = $sep;
187 }
188 40 ▶ if($#{$self->{comments}} > -1 and $#{$self->{titles}} > -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{"comments"};} > -1 and $#{$$self{"titles"};} > -1) 23 17 40
Condition66%2/3
$#{$$self{"comments"};} > -1 and $#{$$self{"titles"};} > -1
!ll&&!rl&&r
17023
MC/DC50%1/2
$#{$$self{"comments"};} > -1 and $#{$$self{"titles"};} > -1
$#{$$self{"comments"};} > -1 $#{$$self{"titles"};} > -1
Truth table66%2/3
A and B
A= $#{$$self{"comments"};} > -1B= $#{$$self{"titles"};} > -1
ABresult
0X0
100
111
189 {
190 23 pop(@{$self->{comments}});
191 }
192 40 return 1; # Yeah, found a number line.
193 }
194 }
195 0 elsif($oc){ return 0; }
196 else
197 {
198 58 ▶ if(${$lr} =~ /^($self->{config}{comregex})($lend)$/)
Branch: 1 branch
BranchTrueFalseTotal
if (${$lr;} =~ /^($self->{"config"}{"comregex"})($lend)$/) 0 58 58
199 {
200 $self->{config}{comchar} = $1
201 0 ▶ unless defined $self->{config}{comchar};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"comchar"} 0 0 0
202 $self->{config}{lineend} = $2
203 0 ▶ unless defined $self->{config}{lineend};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"lineend"} 0 0 0
204 0 return 0;
205 }
206 #first non-empty line is some kind of title or comment
207 #first means: we didn't have content up to now
208 58 ▶ if(${$lr} =~ /^($self->{config}{comregex})($nlend+)($lend)$/)
Branch: 1 branch
BranchTrueFalseTotal
if (${$lr;} =~ /^($self->{"config"}{"comregex"})($nlend+)($lend)$/) 58 0 58
209 {
210 58 ▶ if( defined $self->{title} ){ push(@{$self->{comments}},$2); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'title'}) { } 33 25 58
211 25 else{ $self->{title} = $2; }
212 $self->{config}{lineend} = $3
213 58 ▶ unless defined $self->{config}{lineend};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"lineend"} 6 52 58
214 $self->{config}{comchar} = $1
215 58 ▶ unless defined $self->{config}{comchar};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"comchar"} 23 35 58
216 }
217 #attention: I take " or ' just as quotes, do distinction!
218 58 my $quote = $self->{config}{quotechar};
219 58 ▶ $quote = $quotematch
Branch: 1 branch
BranchTrueFalseTotal
unless defined $quote 58 0 58
220 unless defined $quote;
221 58 ▶ if(${$lr} =~ /^($self->{config}{comregex})($quote)($nlend*\2($seppl)\2*$nlend*)\2*($lend)$/)
Branch: 2 branches
BranchTrueFalseTotal
if (${$lr;} =~ /^($self->{'config'}{'comregex'})($quote)($nlend*\2($seppl)\2*$nlend*)\2*($lend)$/) { } 20 38 58
elsif (${$lr;} =~ /^($self->{'config'}{'comregex'})($quote?)($nlend*)($lend)$/) { } 38 0 38
222 {
223 $self->{config}{quote} = 1
224 20 ▶ unless defined $self->{config}{quote};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"quote"} 20 0 20
225 $self->{config}{quotechar} = $2
226 20 ▶ unless defined $self->{config}{quotechar};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"quotechar"} 20 0 20
227 # "axis title"\t"axis title"\t"..."
228 # allow flexible space in separator
229 20 my $sep = $4;
230 20 my $q = $2;
231 20 my $rest = $3;
232 20 $rest =~ s:$q$::;
233 $sep =~ s:\s+$:\\s+:
234 20 ▶ unless($self->{config}{strict});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"strict"} 20 0 20
235 20 my @ax = split($q.$sep.$q,$rest);
236 20 $self->{titles} = \@ax;
237 $self->{config}{lineend} = $5
238 20 ▶ unless defined $self->{config}{lineend};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"lineend"} 0 20 20
239 $self->{config}{comchar} = $1
240 20 ▶ unless defined $self->{config}{comchar};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"comchar"} 0 20 20
241 }
242 #either no quotes at all or maybe quotes but single item without separator
243 38 elsif(${$lr} =~ /^($self->{config}{comregex})($quote?)($nlend*)($lend)$/)
244 {
245 38 ▶ if($2 ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($2 ne '') { } 0 38 38
246 {
247 $self->{config}{quotechar} = $2
248 0 ▶ unless defined $self->{config}{quotechar};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"quotechar"} 0 0 0
249 $self->{config}{quote} = 1
250 0 ▶ unless defined $self->{config}{quote};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"quote"} 0 0 0
251 }
252 else
253 {
254 38 $self->{guessquote} = 0
255 }
256 $self->{config}{lineend} = $4
257 38 ▶ unless defined $self->{config}{lineend};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"lineend"} 0 38 38
258 $self->{config}{comchar} = $1
259 38 ▶ unless defined $self->{config}{comchar};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"comchar"} 0 38 38
260 38 my $d = $3;
261 38 $d =~ s/$quote$//;
262 38 my @ax = ();
263 38 ▶ if($d =~ /($seppl)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /($seppl)/) { } 24 14 38
264 {
265 24 @ax = split($1, $d);
266 }
267 14 else{ @ax = ($d); }
268 38 $self->{titles} = \@ax;
269 }
270 58 return 0;
271 }
272 }
273  
274 sub get_insep
275 {
276 3 ▶ my $self = shift;
Subroutine get_insep: called CC=2 SCAR=7.2
Pod undocumented
277 return defined $self->{config}{separator}
278 ? $self->{config}{separator}
279 3 ▶ : $default_sep;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'config'}{'separator'} ? : 3 0 3
280 }
281  
282 sub get_outsep
283 {
284 1922 ▶ my $self = shift;
Subroutine get_outsep: called CC=3 SCAR=11.0
Pod undocumented
285 return defined $self->{config}{outsep}
286 ? $self->{config}{outsep}
287 : (
288 defined $self->{config}{separator}
289 ? $self->{config}{separator}
290 1922 ▶ : $default_sep
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'config'}{'outsep'} ? : 10 1912 1922
defined $self->{'config'}{'separator'} ? : 1339 573 1912
291 );
292 }
293  
294 sub get_end
295 {
296 2709 ▶ my $self = shift;
Subroutine get_end: called CC=2 SCAR=6.9
Pod undocumented
297 return defined $self->{config}{lineend}
298 ? $self->{config}{lineend}
299 2709 ▶ : $default_eol;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'config'}{'lineend'} ? : 1318 1391 2709
300 }
301  
302 sub get_quote
303 {
304 25 ▶ my $self = shift;
Subroutine get_quote: called CC=5 SCAR=16.1
Pod undocumented
305 my $want = defined $self->{config}{quote}
306 ? $self->{config}{quote}
307 : ( defined $self->{guessquote}
308 ? $self->{guessquote}
309 25 ▶ : $default_quote );
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'config'}{'quote'} ? : 15 10 25
defined $self->{'guessquote'} ? : 2 8 10
310 return $want
311 ? ( defined $self->{config}{quotechar}
312 ? $self->{config}{quotechar}
313 25 ▶ : $default_quotechar )
Branch: 2 branches
BranchTrueFalseTotal
$want ? : 21 4 25
defined $self->{'config'}{'quotechar'} ? : 12 9 21
314 : '';
315 }
316  
317 sub get_comchar
318 {
319 809 ▶ my $self = shift;
Subroutine get_comchar: called CC=2 SCAR=6.9
Pod undocumented
320 return defined $self->{config}{comchar}
321 ? $self->{config}{comchar}
322 809 ▶ : $default_comchar;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'config'}{'comchar'} ? : 39 770 809
323 }
324  
325 sub line_data
326 {
327 1456 ▶ my $self = shift;
Subroutine line_data: called CC=11 SCAR=24.3
Pod documented
328 1456 ▶ my $lr = ref $_[0] ? $_[0] : \$_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1456 1456
329 1456 my @ar = ();
330 1456 my $zahl = $self->{config}{numregex};
331 # empty lines
332 1456 ▶ return ($self->{config}{empty} ? [] : undef) if(${$lr} =~ /^$lend$/);
Branch: 2 branches
BranchTrueFalseTotal
if ${$lr;} =~ /^$lend$/ 0 1456 1456
$self->{'config'}{'empty'} ? : 0 0 0
333 1456 ▶ if($self->{config}{strict})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'config'}{'strict'}) { } 3 1453 1456
334 {
335 #just split with defined or found separator
336 3 @ar = split($self->get_insep(), ${$lr});
337 #remove line end
338 3 ▶ if($#ar > -1){ $ar[$#ar] =~ s/$lend//o; }
Branch: 1 branch
BranchTrueFalseTotal
if ($#ar > -1) 3 0 3
339 }
340 else
341 {
342 1453 my $l = ${$lr};
343 1453 ▶ if($self->{config}{black})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"black"}) 3 1450 1453
344 {
345 3 $l =~ s/^\s*//;
346 # s/\s*$// deletes the line end -- no problem here
347 3 $l =~ s/\s*$//;
348 }
349 1453 ▶ if($l =~ /^($zahl)(.*)$/){ push(@ar, $1); $l = $2; }else{ return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^($zahl)(.*)$/) { } 1453 0 1453
350 1453 ▶ unless($self->{config}{text})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'config'}{'text'}) { } 1450 3 1453
351 {
352 3 ▶ while($l =~ /^($trenner)($zahl)(.*)$/o)
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^($trenner)($zahl)(.*)$/o) 6 3 9
353 {
354 6 push(@ar, $2);
355 6 $l = $3;
356 }
357 }
358 else
359 {
360 1450 ▶ while($l =~ /^($trenner)($ntrenner)(.*)$/o)
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^($trenner)($ntrenner)(.*)$/o) 2004 1450 3454
361 {
362 2004 push(@ar, $2);
363 2004 $l = $3;
364 }
365 }
366 }
367 1456 return \@ar;
368 }
369  
370 sub data_line
371 {
372 1897 ▶ my $self = shift;
Subroutine data_line: called CC=16 SCAR=33.2
Pod documented
373 1897 my $ar = shift;
374  
375 1897 my $cols_include = shift;
376 1897 my $cols_exclude = shift;
377 1897 my $l = '';
378 1897 my $zahl = $self->{config}{numregex};
379 1897 my $end = $self->get_end();
380 1897 my $sep = $self->get_outsep();
381 1897 my @vals;
382 my @cols;
383 1897 my $i = -1;
384  
385 1897 ▶ unless(defined $cols_include)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cols_include) { } 12 1885 1897
386 {
387 1885 @vals = @{$ar};
388 1885 @cols = (0..$#vals);
389 }
390 else
391 {
392 12 for my $k (@{$cols_include})
393 {
394 28 push(@vals, ($k > -1 and $k < @{$ar})
395 ? $ar->[$k]
396 28 ▶ : $self->{config}{fill});
Branch: 1 branch
BranchTrueFalseTotal
$k > -1 && $k < @{$ar;} ? : 28 0 28
Condition33%1/3
$k > -1 and $k < @{$ar;}
!ll&&!rl&&r
0028
MC/DC0%0/2
$k > -1 and $k < @{$ar;}
$k > -1 $k < @{$ar;}
Truth table33%1/3
A and B
A= $k > -1B= $k < @{$ar;}
ABresult
0X0
100
111
397 28 ▶ push(@cols, $k > -1 ? $k : 0); # ... for numerformat ... arrg
Branch: 1 branch
BranchTrueFalseTotal
$k > -1 ? : 28 0 28
398 }
399 }
400  
401 1897 ▶ if(defined $cols_exclude)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cols_exclude) 0 1897 1897
402 {
403 0 my @oldvals = @vals;
404 0 my @oldcols = @cols;
405 0 @vals = ();
406 0 @cols = ();
407 0 ▶ for(my $i=0; $i<=$#oldvals; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#oldvals) 0 0 0
408 {
409 0 ▶ if(not grep {$_ == $i} @{$cols_exclude})
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ == $i;} @{$cols_exclude;}) 0 0 0
410 {
411 0 push(@vals, $oldvals[$i]);
412 0 push(@cols, $oldcols[$i]);
413 }
414 }
415 }
416  
417 1897 ▶ if(defined $self->{config}{numformat}->[0])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'config'}{'numformat'}[0]) { } 1433 464 1897
418 {
419 1433 foreach my $i (0..$#vals)
420 {
421 3586 my $v = $vals[$i];
422 3586 my $c = $cols[$i];
423 3586 ▶ unless(defined $v){ $l .= $sep; next; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 0 3586 3586
424  
425 3586 my $numform = $self->{config}{numformat}->[$c];
426 3586 ▶ $numform = $self->{config}{numformat}->[0] unless defined $numform;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $numform 2041 1545 3586
427 3586 ▶ if($numform ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($numform ne '') { } 3586 0 3586
428 {
429 3586 ▶ $l .= ($v ne '' and $v =~ /^$zahl$/ ? sprintf($numform, $v) : $v).$sep;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /^$zahl$/ ? : 3584 2 3586
Condition33%1/3
$v ne '' && ($v =~ /^$zahl$/ ? sprintf($numform, $v) : $v)
!ll&&!rl&&r
003586
MC/DC0%0/2
$v ne '' && ($v =~ /^$zahl$/ ? sprintf($numform, $v) : $v)
$v ne '' ($v =~ /^$zahl$/ ? sprintf($numform, $v) : $v)
Truth table33%1/3
A && B
A= $v ne ''B= ($v =~ /^$zahl$/ ? sprintf($numform, $v) : $v)
ABresult
0X0
100
111
430 }
431 0 else{ $l .= $v.$sep; }
432 }
433 1433 $l =~ s/$sep$/$end/;
434 }
435 else
436 {
437 # do I want to care for undefs?
438 # not here ... failure is not communicated from here, you shall handle bad columns externally
439 464 $l = join($sep, @vals).$end;
440 }
441 1897 return \$l;
442 }
443  
444 sub title_line
445 {
446 25 ▶ my $self = shift;
Subroutine title_line: called CC=8 SCAR=24.2
Pod documented
447 25 my $cols_include = shift;
448 25 my $cols_exclude = shift;
449  
450 25 ▶ my @cols = defined $cols_include ? @{$cols_include} : (0..$#{$self->{titles}});
Branch: 1 branch
BranchTrueFalseTotal
defined $cols_include ? : 3 22 25
451 25 ▶ if(defined $cols_exclude)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cols_exclude) 0 25 25
452 {
453 0 my @oldcols = @cols;
454 0 @cols = ();
455 0 ▶ for(my $i=0; $i<=$#oldcols; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#oldcols) 0 0 0
456 {
457 0 ▶ if(not grep {$_ == $i} @{$cols_exclude})
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ == $i;} @{$cols_exclude;}) 0 0 0
458 {
459 0 push(@cols, $oldcols[$i]);
460 }
461 }
462 }
463  
464 25 my $end = $self->get_end();
465 25 my $sep = $self->get_outsep();
466 25 my $com = $self->get_comchar();
467 25 my $q = $self->get_quote();
468 25 my $l = $com.$q;
469 #print STDERR "titles: @{$self->{titles}}\n";
470 #print STDERR "titles for @{$ar}\n" if defined $ar;
471 25 foreach my $k (@cols)
472 {
473 # should match for title containing $q
474 172 ▶ my $t = $k > -1 ? $self->{titles}->[$k] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$k > -1 ? : 172 0 172
475 172 ▶ $t = "" unless defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 172 172
476 172 $l .= $t.$q.$sep.$q;
477 }
478 25 $l =~ s/$q$//;
479 25 $l =~ s/$sep$/$end/;
480 25 return \$l;
481 }
482  
483 sub comment_line
484 {
485 784 ▶ my $self = shift;
Subroutine comment_line: called CC=2 SCAR=6.9
Pod documented
486 784 ▶ my $line = ref $_[0] ? $_[0] : \$_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 783 784
487 784 my $cline = $self->get_comchar().${$line}.$self->get_end();
488 784 return \$cline;
489 }
490  
491 sub chomp_line
492 {
493 0 ▶ my $self = shift;
Subroutine chomp_line: not called CC=3 SCAR=24.8
Pod documented
494 0 ▶ my $string = ref $_[0] ? $_[0] : \$_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
495 0 ▶ if(defined $string)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $string) 0 0 0
496 {
497 0 ${$string} =~ s/$lend$//;
498 }
499 }
500  
501 sub make_naked
502 {
503 35 ▶ my $self = shift;
Subroutine make_naked: called CC=3 SCAR=11.2
Pod documented
504 35 ▶ my $string = ref $_[0] ? $_[0] : \$_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 35 35
505 35 ▶ if(defined $string)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $string) 35 0 35
506 {
507 35 ${$string} =~ s/$lend$//;
508 35 ${$string} =~ s/^$self->{config}{comregex}//;
509 }
510 }
511  
512 # Not well supported, but possible: Text in between numeric data.
513 # To make it a bit safer, this filter will replace everything that would count as separator.
514 # It's only a bit safer... supsequent parsers are supposed to work in strict mode if we're in strict mode here.
515 sub filter_text
516 {
517 0 ▶ my $self = shift;
Subroutine filter_text: not called CC=3 SCAR=24.8
Pod undocumented
518 0 my $match;
519 0 ▶ if($self->{config}{strict})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'config'}{'strict'}) { } 0 0 0
520 {
521 0 my $sep = $self->get_outsep();
522 0 $match = qr/$sep/;
523 }
524 else
525 {
526 0 $match = qr/$trenner/;
527 }
528 0 for(@_){ s:$match:_:g; }
529 }
530  
531 14 1;
532  
533 __END__
« blib/lib/Text/NumericData/App/txdfromtcad.pm ↑ Summary blib/lib/Text/NumericData/App/txdextrema.pm »