blib/lib/Text/NumericData/App.pm

filter: Statementstmt 88.3% 99 / 112 Branchbran 64.4% 49 / 76 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 90.0% 9 / 10 Podpod 66.6% 4 / 6 totaltotal 74.8% 164 / 219 SCAR 42.0
CC
43
Cov
77%
run
23.5
new
12.2
error
12.0
CRAP 66.5
« blib/lib/Text/NumericData/App/txdbaseline.pm ↑ Summary blib/lib/Text/NumericData/App/txdderiv.pm »
1 package Text::NumericData::App;
2  
3 14 ▶use Text::NumericData;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use Config::Param;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # This is just a placeholder because of a past build system bug.
9 # The one and only version for Text::NumericData is kept in
10 # the Text::NumericData module itself.
11 14 our $VERSION = '1';
12 14 $VERSION = eval $VERSION;
13  
14 14 my %shorts = (strict=>'S', text=>'T', numformat=>'N');
15  
16 sub new
17 {
18 19 ▶ my $class = shift;
Subroutine new: called CC=11 SCAR=25.0
Pod undocumented
19 19 my $self = {};
20 19 bless $self, $class;
21 19 $self->{setup} = shift; # main, parconf, pardef, exclude_pars
22 19 $self->{state} = {};
23  
24 # safety check for misspelled keys
25 19 my @known_setup =
26 (qw(
27 parconf
28 pardef
29 exclude
30 filemode
31 pipemode
32 pipe_init
33 pipe_file
34 pipe_prefilter
35 pipe_begin
36 pipe_line
37 pipe_end
38 pipe_allend
39 pipe_header
40 pipe_data
41 pipe_first_data
42 ));
43 19 my @unknown_keys = grep {my $k = $_; not grep {$_ eq $k} @known_setup;} (keys %{$self->{setup}});
44 19 ▶ print STDERR "WARNING: Text::NumericData::App got unknown setup keys (@unknown_keys)\n" if @unknown_keys;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown_keys 0 19 19
45  
46 19 ▶ if($self->{setup}{pipemode})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"setup"}{"pipemode"}) 13 6 19
47 {
48 # Activate pipe processing only on request.
49 13 require Text::ASCIIPipe;
50 }
51  
52 19 ▶ $self->{setup}{parconf} = {} unless defined $self->{setup}{parconf};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"setup"}{"parconf"} 0 19 19
53 # Always return.
54 19 $self->{setup}{parconf}{noexit} = 1;
55  
56 # Lazyness ... why specify this again and again?
57 $self->{setup}{parconf}{copyright} = $Text::NumericData::copyright
58 19 ▶ unless defined $self->{setup}{parconf}{copyright};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"setup"}{"parconf"}{"copyright"} 19 0 19
59 $self->{setup}{parconf}{version} = $Text::NumericData::version
60 19 ▶ unless defined $self->{setup}{parconf}{version};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"setup"}{"parconf"}{"version"} 19 0 19
61 $self->{setup}{parconf}{author} = $Text::NumericData::author
62 19 ▶ unless defined $self->{setup}{parconf}{author};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"setup"}{"parconf"}{"author"} 19 0 19
63  
64 19 ▶ $self->{setup}{pardef} = [] unless defined $self->{setup}{pardef};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"setup"}{"pardef"} 1 18 19
65 19 my $prob = Config::Param::sane_pardef($self->{setup}{parconf}, $self->{setup}{pardef});
66 19 ▶ if($prob ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($prob ne "") 0 19 19
67 {
68 0 print STDERR "Error in given parameter definiton: $prob\nThis is a fatal programming error.\n";
69 0 return undef;
70 }
71 # I'm sure this can be done more elegantly.
72 19 ▶ $self->add_param('Text::NumericData'
Branch: 1 branch
BranchTrueFalseTotal
$self->add_param("Text::NumericData", \%Text::NumericData::defaults, \%Text::NumericData::help) or return undef 0 19 19
73 , \%Text::NumericData::defaults, \%Text::NumericData::help)
74 or return undef;
75 19 ▶ $self->add_param('Text::NumericData::File'
Branch: 1 branch
BranchTrueFalseTotal
$self->add_param("Text::NumericData::File", \%Text::NumericData::File::defaults, \%Text::NumericData::File::help) or return undef 0 19 19
76 , \%Text::NumericData::File::defaults, \%Text::NumericData::File::help)
77 or return undef;
78  
79 19 return $self;
80 }
81  
82 sub add_param
83 {
84 38 ▶ my $self = shift;
Subroutine add_param: called CC=6 SCAR=20.1
Pod undocumented
85 38 my ($pkgname, $defaults, $help) = @_;
86 38 for my $pn (keys %{$defaults})
87 {
88 270 ▶ next if (defined $self->{setup}{exclude} and grep {$_ eq $pn} @{$self->{setup}{exclude}});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"setup"}{"exclude"} and grep {$_ eq $pn;} @{$$self{"setup"}{"exclude"};} 0 270 270
Condition33%1/3
defined $self->{"setup"}{"exclude"} and grep {$_ eq $pn;} @{$$self{"setup"}{"exclude"};}
!ll&&!rl&&r
27000
MC/DC0%0/2
defined $self->{"setup"}{"exclude"} and grep {$_ eq $pn;} @{$$self{"setup"}{"exclude"};}
defined $self->{"setup"}{"exclude"} grep {$_ eq $pn;} @{$$self{"setup"}{"exclude"};}
Truth table33%1/3
A and B
A= defined $self->{"setup"}{"exclude"}B= grep {$_ eq $pn;} @{$$self{"setup"}{"exclude"};}
ABresult
0X0
100
111
89 270 my $help = $help->{$pn};
90 270 ▶ $help = "some $pkgname parameter" unless defined $help;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $help 0 270 270
91 270 $help .= " (from $pkgname)";
92 my $thisdef =
93 {
94 long=>$pn
95 , short=>$shorts{$pn}
96 # No deep copy here, as the calls to Config::Param get copies of this.
97 270 , value=>$defaults->{$pn}
98 , help=>$help
99 };
100 270 ▶ if(Config::Param::sane_pardef($self->{setup}{parconf}, [$thisdef]) ne '')
Branch: 1 branch
BranchTrueFalseTotal
if (Config::Param::sane_pardef($self->{"setup"}{"parconf"}, [$thisdef]) ne "") 0 270 270
101 {
102 0 print STDERR "Unexpected failure to sanitize param definiton for $pn.\n";
103 0 return undef;
104 }
105 270 push(@{$self->{setup}{pardef}}, $thisdef);
106 }
107 38 return 1;
108 }
109  
110 sub run
111 {
112 46 ▶ my ($self, $argv, $in, $out) = @_;
Subroutine run: called CC=18 SCAR=31.6
Pod documented
113 46 ▶ $argv = \@ARGV unless defined $argv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argv 0 46 46
114 46 ▶ $in = \*STDIN unless defined $in;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in 7 39 46
115 46 ▶ $out = \*STDOUT unless defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 46 46
116 46 binmode $in;
117 46 binmode $out;
118 46 $self->{argv} = $argv;
119 46 $self->{in} = $in;
120 46 $self->{out} = $out;
121  
122 46 my $errors;
123 # Ensure that Config::Param cannot mess with our default values by
124 # providing deep copies of configuration. Even if it behaves nice,
125 # better safe than sorry.
126 $self->{param} = Config::Param::get(
127 Storable::dclone($self->{setup}{parconf})
128 , Storable::dclone($self->{setup}{pardef})
129 46 , $self->{argv}, $errors );
130  
131 46 ▶ if(@{$errors})
Branch: 1 branch
BranchTrueFalseTotal
if (@{$errors;}) 0 46 46
132 {
133 0 print STDERR "Stopping here because of parameter parsing errors.\n";
134 0 return 1;
135 }
136 46 ▶ return 0 if($self->{param}{help} or $self->{param}{version});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"param"}{"help"} or $self->{"param"}{"version"} 0 46 46
Condition33%1/3
$self->{"param"}{"help"} or $self->{"param"}{"version"}
l!l&&r!l&&!r
0046
MC/DC0%0/2
$self->{"param"}{"help"} or $self->{"param"}{"version"}
$self->{"param"}{"help"} $self->{"param"}{"version"}
Truth table33%1/3
A or B
A= $self->{"param"}{"help"}B= $self->{"param"}{"version"}
ABresult
1X1
011
000
137  
138 46 ▶ if($self->{setup}{pipemode})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'setup'}{'pipemode'}) { } 39 7 46
139 {
140 39 ▶ if(defined $self->{setup}{pipe_init})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"setup"}{"pipe_init"}) 35 4 39
141 {
142 35 my $err = $self->{setup}{pipe_init}->($self);
143 35 ▶ if($err)
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 35 35
144 {
145 0 print STDERR "Pipe init handler failed, aborting.\n";
146 0 return $err;
147 }
148 }
149 39 ▶ if($self->{setup}{filemode})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'setup'}{'filemode'}) { } 11 28 39
150 {
151 # Not wholly sure about that logic, have to really test and then delete this comment.
152 11 while(1)
153 {
154 11 $self->new_txd();
155 11 my $ret = $self->{txd}->read_all($self->{in});
156 $self->{setup}{pipe_file}->($self)
157 11 ▶ if ($ret >= 0 and defined $self->{setup}{pipe_file});
Branch: 1 branch
BranchTrueFalseTotal
if $ret >= 0 and defined $self->{"setup"}{"pipe_file"} 11 0 11
Condition33%1/3
$ret >= 0 and defined $self->{"setup"}{"pipe_file"}
!ll&&!rl&&r
0011
MC/DC0%0/2
$ret >= 0 and defined $self->{"setup"}{"pipe_file"}
$ret >= 0 defined $self->{"setup"}{"pipe_file"}
Truth table33%1/3
A and B
A= $ret >= 0B= defined $self->{"setup"}{"pipe_file"}
ABresult
0X0
100
111
158 11 ▶ last if $ret <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ret <= 0 11 0 11
159 }
160 $self->{setup}{pipe_allend}->($self)
161 11 ▶ if defined $self->{setup}{pipe_allend};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"setup"}{"pipe_allend"} 0 11 11
162 Text::ASCIIPipe::done($self->{out})
163 11 ▶ if $self->{txd}{config}{pipemode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"txd"}{"config"}{"pipemode"} 0 11 11
164 }
165 else
166 {
167 Text::ASCIIPipe::process
168 (
169 handle => $self
170 ,in => $self->{in}
171 ,out => $self->{out}
172 ,pre => $self->{setup}{pipe_prefilter}
173 ,begin => defined $self->{setup}{pipe_begin} ? $self->{setup}{pipe_begin} : \&new_txd
174 ,line => defined $self->{setup}{pipe_line} ? $self->{setup}{pipe_line} : \&default_line_hook
175 ,end => $self->{setup}{pipe_end}
176 ,allend => $self->{setup}{pipe_allend}
177 28 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'setup'}{'pipe_begin'} ? : 28 0 28
defined $self->{'setup'}{'pipe_line'} ? : 10 18 28
178 }
179 39 return 0;
180 }
181 else
182 {
183 7 $self->new_txd();
184 7 return $self->main();
185 }
186 }
187  
188 sub new_txd
189 {
190 44 ▶ my $self = shift;
Subroutine new_txd: called CC=3 SCAR=11.0
Pod documented
191 44 ▶ require Text::NumericData::File if $self->{setup}{filemode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"setup"}{"filemode"} 11 33 44
192  
193 $self->{txd} = $self->{setup}{filemode}
194 ? Text::NumericData::File->new($self->{param})
195 44 ▶ : Text::NumericData->new($self->{param});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'setup'}{'filemode'} ? : 11 33 44
196  
197 44 $self->{state}{data} = 0;
198 }
199  
200  
201 sub default_line_hook
202 {
203 492 ▶ my $self = shift;
Subroutine default_line_hook: called CC=7 SCAR=19.5
Pod documented
204 492 my $prefix = undef;
205 492 ▶ if(!$self->{state}{data})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"state"}{"data"}) 42 450 492
206 {
207 42 ▶ if($self->{txd}->line_check($_[0]))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'txd'}->line_check($_[0])) { } 18 24 42
208 {
209 18 $self->{state}{data} = 1;
210 $prefix = $self->{setup}{pipe_first_data}->($self, @_)
211 18 ▶ if defined $self->{setup}{pipe_first_data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"setup"}{"pipe_first_data"} 7 11 18
212 }
213 else
214 {
215 $self->{setup}{pipe_header}->($self, @_)
216 24 ▶ if defined $self->{setup}{pipe_header};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"setup"}{"pipe_header"} 24 0 24
217 24 return;
218 }
219 }
220 # If still here, $self->{state}{data} == 1 is implied.
221 $self->{setup}{pipe_data}->($self, @_)
222 468 ▶ if defined $self->{setup}{pipe_data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"setup"}{"pipe_data"} 468 0 468
223 468 ▶ $_[0] = ${$prefix}.$_[0]
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix 7 461 468
224 if defined $prefix;
225 }
226  
227 sub error
228 {
229 0 ▶ my $self = shift;
Subroutine error: not called CC=3 SCAR=24.8
Pod documented
230 0 ▶ print STDERR "$_[0]\n" if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
231 0 ▶ return defined $_[1] ? $_[1] : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
232 }
233  
234 14 1;
235  
236 __END__
« blib/lib/Text/NumericData/App/txdbaseline.pm ↑ Summary blib/lib/Text/NumericData/App/txdderiv.pm »