FormulaEngine/Namespace/Default.pm

filter: Statementstmt 91.1% 413 / 453 Branchbran 62.9% 102 / 162 Conditioncond 63.6% 35 / 55 MC/DCmcdc 41.1% 14 / 34 Subroutinesub 95.8% 114 / 119 Podpod 0.0% 0 / 56 totaltotal 77.1% 678 / 879 CC 103 SCAR 15.9
CC
103
Cov
82%
fn_compare
16.9
simplify_or
13.0
nodeval_or
12.0
CRAP 4.9
« FormulaEngine.pm ↑ Summary FormulaEngine/Parser.pm »
1 package Language::FormulaEngine::Namespace::Default;
2 6 ▶use parent 'Language::FormulaEngine::Namespace';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings FATAL => 'numeric', 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Math::Trig ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # POSIX::round isn't available everywhere
11 6 ▶BEGIN { eval 'use POSIX "round"; 1;' or eval 'sub round { sprintf "%.*f", $_[1]||0, $_[0] }' }
Subroutine BEGIN: called CC=2 SCAR=7.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use POSIX \"round\"; 1;" or eval "sub round { sprintf \"%.*f\", \$_[1]||0, \$_[0] }" 6 0 6
12 6 ▶use Language::FormulaEngine::Error ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use DateTime::Format::Flexible;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # ABSTRACT: Default spreadsheet-like set of functions and behavior
18 6 our $VERSION = '0.08'; # VERSION
19  
20 # No official versioned namespace yet, but this code is for when I publish one.
21  
22 #sub _fake_require {
23 # my $version= shift;
24 # $INC{'Language/FormulaEngine/Namespace/Default/V'.$version.'.pm'}=
25 # $INC{'Language/FormulaEngine/Namespace/Default.pm'};
26 #}
27 #
28 #sub _declare_versioned_namespace {
29 # my ($from_ver, $to_ver, @list)= @_;
30 # no strict 'refs';
31 # my $from_stash= \%{ __PACKAGE__ . '::V' . $from_ver . '::' };
32 # my $to_stash= \%{ __PACKAGE__ . '::V' . $to_ver . '::' };
33 # if (@list) {
34 # for (@list) {
35 # defined $from_stash->{$_} or die "Version $from_ver does not contain method $_";
36 # $to_stash->{$_}= $from_stash->{$_};
37 # }
38 # } else {
39 # for (keys %$from_stash) {
40 # $to_stash->{$_}= $from_stash->{$_}
41 # if defined $from_stash->{$_}{CODE} && !defined $to_stash->{$_};
42 # }
43 # }
44 # @{ __PACKAGE__ . '::V' . $to_ver . '::ISA' }= ( 'Language::FormulaEngine::Namespace' );
45 #}
46 #
47 #_fake_require 0;
48 #@Language::FormulaEngine::Namespace::Default::V0::ISA= ( __PACKAGE__ );
49  
50  
51 6 my @mark_pure;
52 6 *fn_sum= *List::Util::sum0;
53 6 push @mark_pure, qw( fn_sum );
54 2 ▶sub simplify_sum {
Subroutine simplify_sum: called CC=7 SCAR=20.6
Pod undocumented
55 2 my ($self, $node)= @_;
56 2 my ($const, @unknown)= (0);
57 2 for (@{ $node->parameters }) {
58 6 ▶ if ($_->is_constant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->is_constant) { } 4 2 6
59 4 $const += $_->evaluate($self);
60 } else {
61 2 push @unknown, $_;
62 }
63 }
64 2 ▶ return $node if @unknown == @{ $node->parameters } && @unknown > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown == @{$node->parameters;} and @unknown > 1 0 2 2
Condition33%1/3
@unknown == @{$node->parameters;} and @unknown > 1
!ll&&!rl&&r
200
MC/DC0%0/2
@unknown == @{$node->parameters;} and @unknown > 1
@unknown == @{$node->parameters;} @unknown > 1
Truth table33%1/3
A and B
A= @unknown == @{$node->parameters;}B= @unknown > 1
ABresult
0X0
100
111
65 2 ▶ push @unknown, Language::FormulaEngine::Parser::Node::Number->new($const) if $const != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $const != 0 0 2 2
66 2 ▶ return $unknown[0] unless @unknown > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @unknown > 1 2 0 2
67 0 return Language::FormulaEngine::Parser::Node::Call->new($node->function_name, \@unknown);
68 }
69 14 ▶sub perlgen_sum {
Subroutine perlgen_sum: called CC=1 SCAR=0.0
Pod undocumented
70 14 my ($self, $compiler, $node)= @_;
71 14 my @arg_code= map $compiler->perlgen($_), @{$node->parameters};
72 14 return '( '.join(' + ', @arg_code).' )';
73 }
74  
75 2 ▶sub fn_negative :Pure {
Subroutine fn_negative: called CC=2 SCAR=7.2
Pod undocumented
76 2 ▶ @_ == 1 or die "Can only negate a single value, not a list\n";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or die "Can only negate a single value, not a list\n" 2 0 2
77 2 return -$_[0];
78 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶sub perlgen_negative {
Subroutine perlgen_negative: called CC=2 SCAR=7.0
Pod undocumented
80 2 my ($self, $compiler, $node)= @_;
81 2 my @arg_code= map $compiler->perlgen($_), @{$node->parameters};
82 2 ▶ @arg_code == 1 or die "Can only negate a single value, not a list\n";
Branch: 1 branch
BranchTrueFalseTotal
@arg_code == 1 or die "Can only negate a single value, not a list\n" 2 0 2
83 2 return '(-('.$arg_code[0].'))';
84 }
85  
86 6 *fn_mul= *List::Util::product;
87 6 push @mark_pure, qw( fn_mul );
88 4 ▶sub simplify_mul {
Subroutine simplify_mul: called CC=8 SCAR=21.3
Pod undocumented
89 4 my ($self, $node)= @_;
90 4 my ($const, @unknown)= (1);
91 4 for (@{ $node->parameters }) {
92 8 ▶ if ($_->is_constant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->is_constant) { } 5 3 8
93 5 $const *= $_->evaluate($self);
94 } else {
95 3 push @unknown, $_;
96 }
97 }
98 4 ▶ return $node if @unknown == @{ $node->parameters } && @unknown > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown == @{$node->parameters;} and @unknown > 1 0 4 4
Condition33%1/3
@unknown == @{$node->parameters;} and @unknown > 1
!ll&&!rl&&r
400
MC/DC0%0/2
@unknown == @{$node->parameters;} and @unknown > 1
@unknown == @{$node->parameters;} @unknown > 1
Truth table33%1/3
A and B
A= @unknown == @{$node->parameters;}B= @unknown > 1
ABresult
0X0
100
111
99 4 ▶ return Language::FormulaEngine::Parser::Node::Number->new(0) if $const == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $const == 0 2 2 4
100 2 ▶ unshift @unknown, Language::FormulaEngine::Parser::Node::Number->new($const) if $const != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $const != 1 2 0 2
101 2 ▶ return $unknown[0] unless @unknown > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @unknown > 1 0 2 2
102 2 return Language::FormulaEngine::Parser::Node::Call->new($node->function_name, \@unknown);
103 }
104 14 ▶sub perlgen_mul {
Subroutine perlgen_mul: called CC=1 SCAR=0.0
Pod undocumented
105 14 my ($self, $compiler, $node)= @_;
106 14 my @arg_code= map $compiler->perlgen($_), @{$node->parameters};
107 14 return '( '.join(' * ', @arg_code).' )';
108 }
109  
110 1 ▶sub fn_div :Pure {
Subroutine fn_div: called CC=3 SCAR=11.7
Pod undocumented
111 1 ▶ @_ == 2 or die "div() takes exactly two arguments\n";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die "div() takes exactly two arguments\n" 1 0 1
112 1 ▶ $_[1] or die "division by zero\n";
Branch: 1 branch
BranchTrueFalseTotal
$_[1] or die "division by zero\n" 1 0 1
113 1 return $_[0] / $_[1];
114 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
115 4 ▶sub perlgen_div {
Subroutine perlgen_div: called CC=1 SCAR=0.0
Pod undocumented
116 4 my ($self, $compiler, $node)= @_;
117 4 my @arg_code= map $compiler->perlgen($_), @{$node->parameters};
118 4 return '( '.join(' / ', @arg_code).' )';
119 }
120  
121 3 ▶sub nodeval_and :Pure { # customize nodeval_ to provide lazy evaluation of arguments
Subroutine nodeval_and: called CC=3 SCAR=11.0
Pod undocumented
122 3 my ($self, $node)= @_;
123 $_->evaluate($self) or return 0
124 3 ▶ for @{ $node->parameters };
Branch: 1 branch
BranchTrueFalseTotal
$_->evaluate($self) or return 0 5 1 6
125 2 return 1;
126 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
127 1 ▶sub simplify_and {
Subroutine simplify_and: called CC=6 SCAR=22.9
Pod undocumented
128 1 my ($self, $node)= @_;
129 1 my @unknown;
130 1 for (@{ $node->parameters }) {
131 2 ▶ if ($_->is_constant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->is_constant) { } 2 0 2
132 # true constant causes expression to always evaluate true
133 2 ▶ return Language::FormulaEngine::Parser::Node::Number->new(0)
Branch: 1 branch
BranchTrueFalseTotal
unless $_->evaluate($self) 0 2 2
134 unless $_->evaluate($self);
135 } else {
136 0 push @unknown, $_;
137 }
138 }
139 1 ▶ return Language::FormulaEngine::Parser::Node::Number->new(1) unless @unknown;
Branch: 1 branch
BranchTrueFalseTotal
unless @unknown 1 0 1
140 0 ▶ return $node if @unknown == @{ $node->parameters };
Branch: 1 branch
BranchTrueFalseTotal
if @unknown == @{$node->parameters;} 0 0 0
141 0 return Language::FormulaEngine::Parser::Node::Call->new($node->function_name, \@unknown);
142 }
143 4 ▶sub perlgen_and {
Subroutine perlgen_and: called CC=1 SCAR=0.0
Pod undocumented
144 4 my ($self, $compiler, $node)= @_;
145 4 my @arg_code= map $compiler->perlgen($_), @{$node->parameters};
146 4 return '( ('.join(' and ', @arg_code).')? 1 : 0)';
147 }
148  
149 0 ▶sub nodeval_or :Pure {
Subroutine nodeval_or: not called CC=3 SCAR=24.8
Pod undocumented
150 0 my ($self, $node)= @_;
151 $_->evaluate($self) and return 1
152 0 ▶ for @{ $node->parameters };
Branch: 1 branch
BranchTrueFalseTotal
$_->evaluate($self) and return 1 0 0 0
153 0 return 0;
154 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
155 2 ▶sub simplify_or {
Subroutine simplify_or: called CC=6 SCAR=25.6
Pod undocumented
156 2 my ($self, $node)= @_;
157 2 my @unknown;
158 2 for (@{ $node->parameters }) {
159 2 ▶ if ($_->is_constant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->is_constant) { } 2 0 2
160 # true constant causes expression to always evaluate true
161 2 ▶ return Language::FormulaEngine::Parser::Node::Number->new(1)
Branch: 1 branch
BranchTrueFalseTotal
if $_->evaluate($self) 2 0 2
162 if $_->evaluate($self);
163 } else {
164 0 push @unknown, $_;
165 }
166 }
167 0 ▶ return Language::FormulaEngine::Parser::Node::Number->new(0) unless @unknown;
Branch: 1 branch
BranchTrueFalseTotal
unless @unknown 0 0 0
168 0 ▶ return $node if @unknown == @{ $node->parameters };
Branch: 1 branch
BranchTrueFalseTotal
if @unknown == @{$node->parameters;} 0 0 0
169 0 return Language::FormulaEngine::Parser::Node::Call->new($node->function_name, \@unknown);
170 }
171 0 ▶sub perlgen_or {
Subroutine perlgen_or: not called CC=1 SCAR=6.9
Pod undocumented
172 0 my ($self, $compiler, $node)= @_;
173 0 my @arg_code= map $compiler->perlgen($_), @{$node->parameters};
174 0 return '( ('.join(' or ', @arg_code).')? 1 : 0)';
175 }
176  
177 2 ▶sub fn_not :Pure {
Subroutine fn_not: called CC=3 SCAR=11.1
Pod undocumented
178 2 ▶ @_ == 1 or die "Too many arguments to 'not'\n";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or die "Too many arguments to 'not'\n" 2 0 2
179 2 ▶ return $_[0]? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 1 2
180 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
181 3 ▶sub perlgen_not {
Subroutine perlgen_not: called CC=2 SCAR=7.0
Pod undocumented
182 3 my ($self, $compiler, $node)= @_;
183 3 my @arg_code= map $compiler->perlgen($_), @{$node->parameters};
184 3 ▶ @arg_code == 1 or die "Too many arguments to 'not'\n";
Branch: 1 branch
BranchTrueFalseTotal
@arg_code == 1 or die "Too many arguments to 'not'\n" 3 0 3
185 3 return '('.$arg_code[0].'? 0 : 1)';
186 }
187  
188 21 ▶sub fn_compare :Pure {
Subroutine fn_compare: called CC=15 SCAR=28.3
Pod undocumented
189 21 my $left= shift;
190 21 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 27 15 42
191 27 my $op= shift;
192 27 my $right= shift;
193 27 ▶ my $numeric= Scalar::Util::looks_like_number($left) && Scalar::Util::looks_like_number($right);
Condition66%2/3
&Scalar::Util::looks_like_number($left) && &Scalar::Util::looks_like_number($right)
!ll&&!rl&&r
0225
MC/DC50%1/2
&Scalar::Util::looks_like_number($left) && &Scalar::Util::looks_like_number($right)
&Scalar::Util::looks_like_number($left) &Scalar::Util::looks_like_number($right)
Truth table66%2/3
A && B
A= &Scalar::Util::looks_like_number($left)B= &Scalar::Util::looks_like_number($right)
ABresult
0X0
100
111
194 27 ▶ if ($op eq '==' or $op eq '!=') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq '==' or $op eq '!=') { } 2 25 27
elsif ($op eq '>=' or $op eq '<') { } 6 19 25
elsif ($op eq '<=' or $op eq '>') { } 19 0 19
Condition66%2/3
$op eq "==" or $op eq "!="
l!l&&r!l&&!r
2025
MC/DC50%1/2
$op eq "==" or $op eq "!="
$op eq "==" $op eq "!="
Truth table66%2/3
A or B
A= $op eq "=="B= $op eq "!="
ABresult
1X1
011
000
195 2 ▶ return 0 unless ($numeric? ($left == $right) : ($left eq $right)) == ($op eq '==');
Branch: 2 branches
BranchTrueFalseTotal
unless ($numeric ? $left == $right : $left eq $right) == ($op eq "==") 0 2 2
$numeric ? : 2 0 2
196 }
197 ▶ elsif ($op eq '>=' or $op eq '<') {
Condition100%3/3
$op eq ">=" or $op eq "<"
l!l&&r!l&&!r
2419
MC/DC100%2/2
$op eq ">=" or $op eq "<"
$op eq ">=" $op eq "<"
Truth table100%3/3
A or B
A= $op eq ">="B= $op eq "<"
ABresult
1X1
011
000
198 6 ▶ return 0 unless ($numeric? ($left >= $right) : ($left ge $right)) == ($op eq '>=');
Branch: 2 branches
BranchTrueFalseTotal
unless ($numeric ? $left >= $right : $left ge $right) == ($op eq ">=") 0 6 6
$numeric ? : 4 2 6
199 }
200 ▶ elsif ($op eq '<=' or $op eq '>') {
Condition66%2/3
$op eq "<=" or $op eq ">"
l!l&&r!l&&!r
2170
MC/DC0%0/2
$op eq "<=" or $op eq ">"
$op eq "<=" $op eq ">"
Truth table66%2/3
A or B
A= $op eq "<="B= $op eq ">"
ABresult
1X1
011
000
201 19 ▶ return 0 unless ($numeric? ($left <= $right) : ($left le $right)) == ($op eq '<=');
Branch: 2 branches
BranchTrueFalseTotal
unless ($numeric ? $left <= $right : $left le $right) == ($op eq "<=") 6 13 19
$numeric ? : 19 0 19
202 }
203 else {
204 0 die "Unhandled operator '$op' in compare()\n";
205 }
206 21 $left= $right;
207 }
208 15 return 1;
209 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
210  
211  
212 2 ▶sub fn_choose :Pure {
Subroutine fn_choose: called CC=3 SCAR=13.1
Pod undocumented
213 2 ▶ $_[0] > 0 and $_[0] < @_ or die "CHOSE() selector out of bounds ($_[0])";
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > 0 and $_[0] < @_ or die "CHOSE() selector out of bounds ($_[0])" 2 0 2
Condition33%1/3
$_[0] > 0 and $_[0] < @_
!ll&&!rl&&r
002
MC/DC0%0/2
$_[0] > 0 and $_[0] < @_
$_[0] > 0 $_[0] < @_
Truth table33%1/3
A and B
A= $_[0] > 0B= $_[0] < @_
ABresult
0X0
100
111
214 2 return $_[$_[0]];
215 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
216  
217 2 ▶sub nodeval_if :Pure { # customize nodeval_ to provide lazy evaluation of arguments
Subroutine nodeval_if: called CC=3 SCAR=11.0
Pod undocumented
218 2 my ($self, $node)= @_;
219 2 ▶ @{$node->parameters} == 3 or die "IF(test, when_true, when_false) requires all 3 parameters\n";
Branch: 1 branch
BranchTrueFalseTotal
@{$node->parameters;} == 3 or die "IF(test, when_true, when_false) requires all 3 parameters\n" 2 0 2
220 2 my $bool= $node->parameters->[0]->evaluate($self);
221 2 ▶ return $node->parameters->[$bool? 1 : 2]->evaluate($self);
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 1 1 2
222 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
223 4 ▶sub perlgen_if {
Subroutine perlgen_if: called CC=2 SCAR=7.0
Pod undocumented
224 4 my ($self, $compiler, $node)= @_;
225 4 my @arg_code= map $compiler->perlgen($_), @{$node->parameters};
226 4 ▶ @arg_code == 3 or die "IF(test, when_true, when_false) requires all 3 parameters\n";
Branch: 1 branch
BranchTrueFalseTotal
@arg_code == 3 or die "IF(test, when_true, when_false) requires all 3 parameters\n" 4 0 4
227 4 return '( '.$arg_code[0].'? '.$arg_code[1].' : '.$arg_code[2].' )';
228 }
229  
230 6 ▶sub nodeval_iferror :Pure {
Subroutine nodeval_iferror: called CC=1 SCAR=0.0
Pod undocumented
231 6 my ($self, $node)= @_;
232 6 my $ret;
233 6 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
234 6 $ret= $node->parameters->[0]->evaluate($self);
235 5 ▶ } catch {
Subroutine __ANON__: called CC=2 SCAR=7.2
236 5 my $err= $node->parameters->[1];
237 5 ▶ $ret= defined $err? $err->evaluate($self) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 5 0 5
238 6 };
239 5 return $ret;
240 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
241 6 ▶sub perlgen_iferror {
Subroutine perlgen_iferror: called CC=1 SCAR=0.0
Pod undocumented
242 6 my ($self, $compiler, $node)= @_;
243 6 my @arg_code= map $compiler->perlgen($_), @{$node->parameters};
244 6 return '(do { local $@; my $x; eval { $x=('.$arg_code[0].'); 1 }? $x : ('.$arg_code[1].') })';
245 }
246  
247 3 ▶sub nodeval_ifs :Pure {
Subroutine nodeval_ifs: called CC=4 SCAR=13.9
Pod undocumented
248 3 my ($self, $node)= @_;
249 3 ▶ (my @todo= @{$node->parameters}) & 1
Branch: 1 branch
BranchTrueFalseTotal
(my(@todo) = @{$node->parameters;}) & 1 and die "IFS(cond, val, ...) requires an even number of parameters\n" 0 3 3
250 and die "IFS(cond, val, ...) requires an even number of parameters\n";
251 3 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 6 1 7
252 6 my ($cond, $val)= splice @todo, 0, 2;
253 6 ▶ return $val->evaluate($self) if $cond->evaluate($self);
Branch: 1 branch
BranchTrueFalseTotal
if $cond->evaluate($self) 2 4 6
254 }
255 1 die "IFS() had no true conditions\n";
256 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
257 3 ▶sub perlgen_ifs {
Subroutine perlgen_ifs: called CC=3 SCAR=11.0
Pod undocumented
258 3 my ($self, $compiler, $node)= @_;
259 3 ▶ (my @arg_code= map $compiler->perlgen($_), @{$node->parameters}) & 1
Branch: 1 branch
BranchTrueFalseTotal
(my(@arg_code) = map($compiler->perlgen($_), @{$node->parameters;})) & 1 and die "IFS(cond, val, ...) requires an even number of parameters\n" 0 3 3
260 and die "IFS(cond, val, ...) requires an even number of parameters\n";
261 3 my $expr= '(';
262 3 ▶ while (@arg_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg_code) 6 3 9
263 6 my ($cond, $val)= splice @arg_code, 0, 2;
264 6 $expr .= "($cond)? ($val) : ";
265 }
266 3 $expr .= 'die "IFS() had no true conditions\n")';
267 3 return $expr;
268 }
269  
270 2 ▶sub fn_na :Pure {
Subroutine fn_na: called CC=1 SCAR=0.0
Pod undocumented
271 2 die ErrNA("NA");
272 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
273  
274  
275 6 *fn_abs= *CORE::abs;
276 6 *fn_acos= *Math::Trig::acos;
277 6 *fn_acot= *Math::Trig::acot;
278 6 *fn_asin= *Math::Trig::asin;
279 6 *fn_atan= *Math::Trig::atan;
280 6 push @mark_pure, qw( fn_abs fn_acos fn_acot fn_asin fn_atan );
281  
282 2 ▶sub fn_atan2 :Pure {
Subroutine fn_atan2: called CC=1 SCAR=0.0
Pod undocumented
283 # Perl differs in argument order from popular spreadsheet programs
284 2 atan2($_[1], $_[0])
285 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
286  
287 4 ▶sub fn_average :Pure {
Subroutine fn_average: called CC=1 SCAR=0.0
Pod undocumented
288 4 List::Util::sum0(@_) / @_;
289 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
290  
291 4 ▶sub fn_base :Pure {
Subroutine fn_base: called CC=5 SCAR=16.1
Pod undocumented
292 4 my ($num, $radix, $min_length)= @_;
293 4 my $digits= '';
294 4 ▶ while ($num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 0) 16 4 20
295 6 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
296 16 ($num, my $digit)= ($num / $radix, $num % $radix);
297 16 ▶ $digits= chr(($digit < 10? 48 : 65) + $digit) . $digits;
Branch: 1 branch
BranchTrueFalseTotal
$digit < 10 ? : 16 0 16
298 }
299 4 ▶ my $pad= ($min_length||0) - length $digits;
Condition100%2/2
$min_length || 0
l!l
22
MC/DC100%1/1
$min_length || 0
$min_length
Truth table100%2/2
A
A= $min_length
Aresult
11
00
300 4 ▶ return $pad > 0? '0'x$pad . $digits : $digits;
Branch: 1 branch
BranchTrueFalseTotal
$pad > 0 ? : 2 2 4
301 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
302  
303 6 *fn_cos= *CORE::cos;
304 6 *fn_degrees= *Math::Trig::rad2deg;
305 6 *fn_exp= *CORE::exp;
306 6 push @mark_pure, qw( fn_cos fn_degrees fn_exp );
307  
308 6 ▶sub fn_fact :Pure {
Subroutine fn_fact: called CC=3 SCAR=11.0
Pod undocumented
309 6 my $n= int($_[0]);
310 6 ▶ return 1 unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 2 4 6
311 4 ▶ $n > 0 or die ErrNUM("Can't compute factorial of negative number '$n'");
Branch: 1 branch
BranchTrueFalseTotal
$n > 0 or die ErrNUM("Can't compute factorial of negative number '${n}'") 4 0 4
312 4 List::Util::product(1 .. $n);
313 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
314  
315 6 *fn_min= *List::Util::min;
316 6 *fn_max= *List::Util::max;
317 6 push @mark_pure, qw( fn_min fn_max );
318 8 ▶sub fn_mod :Pure {
Subroutine fn_mod: called CC=2 SCAR=7.1
Pod undocumented
319 8 my ($num, $modulo)= @_;
320 8 ▶ $modulo+0 or die ErrNUM("MOD($num, $modulo): can't claculate modulus-0");
Branch: 1 branch
BranchTrueFalseTotal
$modulo + 0 or die ErrNUM("MOD($num, $modulo): can't claculate modulus-0") 8 0 8
321 8 $num % $modulo;
322 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
323  
324 6 *fn_pi= *Math::Trig::pi;
325 6 push @mark_pure, qw( fn_pi );
326  
327 6 ▶sub fn_round :Pure {
Subroutine fn_round: called CC=2 SCAR=6.9
Pod undocumented
328 6 my ($num, $digits)= @_;
329 6 ▶ my $scale= 0.1 ** ($_[1] || 0);
Condition100%2/2
$_[1] || 0
l!l
42
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
330 6 return round($num / $scale) * $scale;
331 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
332  
333 6 our $epsilon= 5e-14; # fudge factor for avoiding floating point rounding errors
334 18 ▶sub fn_ceiling :Pure {
Subroutine fn_ceiling: called CC=2 SCAR=6.9
Pod undocumented
335 18 my ($num, $step)= @_;
336 18 ▶ $step= 1 unless defined $step;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $step 2 16 18
337 18 return POSIX::ceil($num / $step - $epsilon) * $step;
338 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
339 18 ▶sub fn_floor :Pure {
Subroutine fn_floor: called CC=2 SCAR=6.9
Pod undocumented
340 18 my ($num, $step)= @_;
341 18 ▶ $step= 1 unless defined $step;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $step 2 16 18
342 18 return POSIX::floor($num / $step + $epsilon) * $step;
343 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
344 8 ▶sub fn_roundup :Pure {
Subroutine fn_roundup: called CC=2 SCAR=6.9
Pod undocumented
345 8 ▶ fn_ceiling($_[0], 0.1 ** ($_[1] || 0));
Condition100%2/2
$_[1] || 0
l!l
44
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
346 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
347 8 ▶sub fn_rounddown :Pure {
Subroutine fn_rounddown: called CC=2 SCAR=6.9
Pod undocumented
348 8 ▶ fn_floor($_[0], 0.1 ** ($_[1] || 0));
Condition100%2/2
$_[1] || 0
l!l
44
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
349 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
350  
351 2 ▶sub fn_power :Pure {
Subroutine fn_power: called CC=2 SCAR=7.2
Pod undocumented
352 2 ▶ @_ == 2 or die ErrInval("POWER() takes 2 arguments");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or die ErrInval("POWER() takes 2 arguments") 2 0 2
353 2 return $_[0] ** $_[1];
354 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
355  
356 6 *fn_rand= *CORE::rand;
357 6 *fn_sin= *CORE::sin;
358 6 *fn_sqrt= *CORE::sqrt;
359 6 *fn_tan= *Math::Trig::tan;
360 6 push @mark_pure, qw( fn_sin fn_sqrt fn_tan );
361  
362  
363 6 *fn_char= *CORE::chr;
364 14 ▶sub fn_clean {
Subroutine fn_clean: called CC=1 SCAR=0.0
Pod undocumented
365 14 my $str= shift;
366 14 $str =~ s/[[:^print:]]+//g;
367 14 $str;
368 }
369 6 *fn_code= *CORE::ord;
370 6 *fn_upper= *CORE::uc;
371 6 *fn_lower= *CORE::lc;
372 6 push @mark_pure, qw( fn_char fn_code fn_upper fn_lower );
373  
374 0 ▶sub fn_replace :Pure {
Subroutine fn_replace: not called CC=2 SCAR=17.9
Pod undocumented
375 0 ▶ @_ == 4 or die ErrInval("REPLACE() takes 4 arguments");
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or die ErrInval("REPLACE() takes 4 arguments") 0 0 0
376 0 my ($text, $ofs, $n, $newtext)= @_;
377 0 substr($text, $ofs, $n)= $newtext;
378 0 return $text;
379 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
380  
381 6 *fn_substr= *CORE::substr;
382 6 *fn_len= *CORE::length;
383  
384 0 ▶sub fn_concatenate :Pure {
Subroutine fn_concatenate: not called CC=1 SCAR=6.9
Pod undocumented
385 0 join '', @_;
386 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
387 6 *fn_concat= *fn_concatenate;
388 6 *fn_join= *CORE::join;
389  
390 6 push @mark_pure, qw( fn_substr fn_len fn_join );
391  
392 0 ▶sub fn_find :Pure {
Subroutine fn_find: not called CC=3 SCAR=24.8
Pod undocumented
393 0 my ($needle, $haystack, $ofs)= @_;
394 0 ▶ $ofs= 1 unless $ofs && $ofs > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $ofs and $ofs > 0 0 0 0
Condition0%0/3
$ofs and $ofs > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$ofs and $ofs > 0
$ofs $ofs > 0
Truth table0%0/3
A and B
A= $ofsB= $ofs > 0
ABresult
0X0
100
111
395 0 return index($haystack, $needle, $ofs) + 1;
396 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
397  
398 10 ▶sub fn_fixed :Pure {
Subroutine fn_fixed: called CC=10 SCAR=23.1
Pod undocumented
399 10 my ($number, $places, $comma)= @_;
400 10 ▶ $places= 2 unless defined $places;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $places 2 8 10
401 10 ▶ $comma= ',' unless defined $comma && (!$comma or $comma eq '.');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $comma and !$comma || $comma eq "." 6 4 10
Condition66%4/6
defined $comma and !$comma || $comma eq "."
!ll&&!rl&&r
604
not $comma or $comma eq "."
l!l&&r!l&&!r
220
MC/DC33%1/3
defined $comma and !$comma || $comma eq "."
defined $comma not $comma $comma eq "."
Truth table75%3/4
A and not(B) or C
A= defined $commaB= not $commaC= $comma eq "."
ABCresult
0XX0
1000
11X1
1011
402 10 ▶ $number= $places > 0? sprintf("%.*f", $places, $number) : fn_round($number, $places);
Branch: 1 branch
BranchTrueFalseTotal
$places > 0 ? : 8 2 10
403 10 ▶ if ($comma) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comma) 8 2 10
404 8 ▶ $number =~ s/\./,/ if $comma eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $comma eq "." 2 6 8
405 8 ▶ my $tmp= reverse substr($number, 0, $places > 0? -($places+1) : length $number);
Branch: 1 branch
BranchTrueFalseTotal
$places > 0 ? : 6 2 8
406 8 $tmp =~ s/(\d\d\d)(?=\d)/$1$comma/g;
407 8 ▶ substr($number, 0, $places > 0? -($places+1) : length $number)= reverse $tmp;
Branch: 1 branch
BranchTrueFalseTotal
$places > 0 ? : 6 2 8
408 }
409 10 return $number;
410 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
411  
412 12 ▶sub fn_trim :Pure {
Subroutine fn_trim: called CC=1 SCAR=0.0
Pod undocumented
413 12 my $str= shift;
414 12 $str =~ s/\p{Space}+/ /g;
415 12 $str =~ s/^ //;
416 12 $str =~ s/ $//;
417 12 $str;
418 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
419  
420  
421 82 ▶sub fn_datevalue :Pure {
Subroutine fn_datevalue: called CC=3 SCAR=11.0
Pod undocumented
422 82 my $date= shift;
423 82 ▶ return $date if ref $date && ref($date)->isa('DateTime');
Branch: 1 branch
BranchTrueFalseTotal
if ref $date and ref($date)->isa("DateTime") 48 34 82
Condition66%2/3
ref $date and ref($date)->isa("DateTime")
!ll&&!rl&&r
34048
MC/DC50%1/2
ref $date and ref($date)->isa("DateTime")
ref $date ref($date)->isa("DateTime")
Truth table66%2/3
A and B
A= ref $dateB= ref($date)->isa("DateTime")
ABresult
0X0
100
111
424 34 ▶ try { DateTime::Format::Flexible->parse_datetime($date) }
Subroutine __ANON__: called CC=1 SCAR=0.0
425 34 ▶ catch { die ErrInval("Not a date: '$date'") };
Subroutine __ANON__: called CC=1 SCAR=0.0
426 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
427 6 ▶BEGIN { *_date= *fn_datevalue; } # for convenience
Subroutine BEGIN: called CC=1 SCAR=0.0
428 4 ▶sub fn_date :Pure {
Subroutine fn_date: called CC=1 SCAR=0.4
Pod undocumented
429 4 my ($y, $m, $d)= @_;
430 4 ▶ try { DateTime->new(year => $y, month => $m, day => $d) }
Subroutine __ANON__: called CC=1 SCAR=0.0
431 4 ▶ catch { die ErrInval(ref $_ && $_->can("message")? $_->message : "$_") };
Subroutine __ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_->can('message') ? : 2 0 2
Condition33%1/3
ref $_ and $_->can("message")
!ll&&!rl&&r
002
MC/DC0%0/2
ref $_ and $_->can("message")
ref $_ $_->can("message")
Truth table33%1/3
A and B
A= ref $_B= $_->can("message")
ABresult
0X0
100
111
432 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
433  
434 2 ▶sub fn_datedif :Pure {
Subroutine fn_datedif: called CC=5 SCAR=21.6
Pod undocumented
435 2 my ($start, $end, $unit)= @_;
436 2 ▶ $unit= uc($unit || '');
Condition50%1/2
$unit || ''
l!l
20
MC/DC0%0/1
$unit || ''
$unit
Truth table50%1/2
A
A= $unit
Aresult
11
00
437 0 ▶ if ($unit eq 'Y') { return _date($end)->delta_md(_date($start))->in_units('years') }
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "Y") 0 2 2
438 2 ▶ if ($unit eq 'M') { return _date($end)->delta_md(_date($start))->in_units('months') }
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "M") 2 0 2
439 0 ▶ if ($unit eq 'D') { return _date($end)->delta_days(_date($start))->in_units('days') }
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "D") 0 0 0
440 0 die ErrInval "Unsupported datedif unit '$unit'";
441 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
442 8 ▶sub fn_day :Pure {
Subroutine fn_day: called CC=1 SCAR=0.0
Pod undocumented
443 8 _date($_[0])->day
444 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
445 4 ▶sub fn_days :Pure {
Subroutine fn_days: called CC=2 SCAR=6.9
Pod undocumented
446 4 my ($end, $start)= ( _date($_[0]), _date($_[1]) );
447 4 my $n= $end->delta_days($start)->in_units('days');
448 4 ▶ return $end > $start? $n : -$n;
Branch: 1 branch
BranchTrueFalseTotal
$end > $start ? : 2 2 4
449 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
450 6 ▶sub fn_eomonth :Pure {
Subroutine fn_eomonth: called CC=2 SCAR=6.9
Pod undocumented
451 6 my ($start, $m_ofs)= @_;
452 6 ▶ $m_ofs= 0 unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 2 4 6
453 6 _date($start)->clone->add(months => $m_ofs+1)->truncate(to => 'month')->subtract(days => 1);
454 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
455 4 ▶sub fn_hour :Pure {
Subroutine fn_hour: called CC=1 SCAR=0.0
Pod undocumented
456 4 _date($_[0])->hour
457 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
458 2 ▶sub fn_minute :Pure {
Subroutine fn_minute: called CC=1 SCAR=0.0
Pod undocumented
459 2 _date($_[0])->minute
460 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
461 2 ▶sub fn_month :Pure {
Subroutine fn_month: called CC=1 SCAR=0.0
Pod undocumented
462 2 _date($_[0])->month
463 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
464 2 ▶sub fn_now {
Subroutine fn_now: called CC=1 SCAR=0.0
Pod undocumented
465 2 DateTime->now;
466 }
467 2 ▶sub fn_second :Pure {
Subroutine fn_second: called CC=1 SCAR=0.0
Pod undocumented
468 2 _date($_[0])->second
469 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
470 2 ▶sub fn_today {
Subroutine fn_today: called CC=1 SCAR=0.0
Pod undocumented
471 2 DateTime->now->truncate(to => 'day');
472 }
473 44 ▶sub fn_weekday :Pure {
Subroutine fn_weekday: called CC=8 SCAR=21.2
Pod undocumented
474 44 my ($date, $standard)= @_;
475 44 my $day_mon1= _date($date)->day_of_week;
476 44 ▶ return $day_mon1 % 7 + 1 if !$standard or $standard == 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $standard or $standard == 1 8 36 44
Condition100%3/3
not $standard or $standard == 1
l!l&&r!l&&!r
4436
MC/DC100%2/2
not $standard or $standard == 1
not $standard $standard == 1
Truth table100%3/3
not(A) or B
A= not $standardB= $standard == 1
ABresult
1X1
011
000
477 36 ▶ return $day_mon1 if $standard == 2 or $standard == 11;
Branch: 1 branch
BranchTrueFalseTotal
if $standard == 2 or $standard == 11 8 28 36
Condition100%3/3
$standard == 2 or $standard == 11
l!l&&r!l&&!r
4428
MC/DC100%2/2
$standard == 2 or $standard == 11
$standard == 2 $standard == 11
Truth table100%3/3
A or B
A= $standard == 2B= $standard == 11
ABresult
1X1
011
000
478 28 ▶ return $day_mon1-1 if $standard == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $standard == 3 4 24 28
479 24 ▶ return ($day_mon1 - ($standard - 10)) % 7 + 1 if $standard >= 12 && $standard <= 17;
Branch: 1 branch
BranchTrueFalseTotal
if $standard >= 12 and $standard <= 17 24 0 24
Condition33%1/3
$standard >= 12 and $standard <= 17
!ll&&!rl&&r
0024
MC/DC0%0/2
$standard >= 12 and $standard <= 17
$standard >= 12 $standard <= 17
Truth table33%1/3
A and B
A= $standard >= 12B= $standard <= 17
ABresult
0X0
100
111
480 0 die ErrInval("No known weekday standard '$standard'");
481 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
482 2 ▶sub fn_year :Pure {
Subroutine fn_year: called CC=1 SCAR=0.0
Pod undocumented
483 2 _date($_[0])->year
484 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
485  
486 # Perl older than 5.16 can't actually reference the functions in CORE:: namespace.
487 # For example, perl -e 'my $sub= sub { CORE::ord(shift) }; print $sub->("A")' works but
488 # perl -e 'my $sub= sub { &CORE::ord }; print $sub->("A")' does not. Neither does
489 # perl -e 'CORE->can("ord")->("A")', nor does *fn_foo= *CORE::foo used above.
490 # I could of course just wrap each core function with a function defined in this
491 # package, but it would be a needless performance hit for modern perl, and clutter
492 # the code above.
493 6 ▶if ($] < 5.016) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.016") 0 6 6
494 0 require Sub::Util;
495 0 my $stash= \%Language::FormulaEngine::Namespace::Default::;
496 0 for my $fn (grep /^fn_/, keys %$stash) {
497 0 my $symname= "$stash->{$fn}";
498 0 ▶ next unless $symname =~ s/^\*CORE/CORE/;
Branch: 1 branch
BranchTrueFalseTotal
unless $symname =~ s/^\*CORE/CORE/ 0 0 0
499 #print "# Stash $fn is $symname\n";
500 # prototypes make this annoying
501 0 ▶ my $code= $symname eq 'CORE::substr'? "sub { substr(shift, \@_) }"
Branch: 2 branches
BranchTrueFalseTotal
$symname eq 'CORE::substr' ? : 0 0 0
$symname eq 'CORE::join' ? : 0 0 0
502 : $symname eq 'CORE::join'? "sub { join(shift, \@_) }"
503 : "sub { $symname(shift) }";
504 0 ▶ my $sub= eval $code or die "$@";
Branch: 1 branch
BranchTrueFalseTotal
my $sub = eval $code or die "$@" 0 0 0
505 6 ▶ no strict 'refs'; no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
506 # The name of the sub needs to remain as CORE::foo else test cases will fail
507 0 *$fn= Sub::Util::set_subname($symname, $sub);
508 }
509 }
510  
511 __PACKAGE__->MODIFY_CODE_ATTRIBUTES(__PACKAGE__->can($_), 'Pure')
512 6 for @mark_pure;
513  
514 6 1;
515  
516 __END__
« FormulaEngine.pm ↑ Summary FormulaEngine/Parser.pm »