| 1 | package Zapp::Formula; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Formula interpreter | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | # "That was almost the perfect crime. But you forgot one thing: rock | ||||||||||||||||||||||||||||||||
| 5 | # crushes scissors. But paper covers rock … and scissors cuts paper!" | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 8 | #pod | ||||||||||||||||||||||||||||||||
| 9 | #pod my $f = Zapp::Formula->new; | ||||||||||||||||||||||||||||||||
| 10 | #pod | ||||||||||||||||||||||||||||||||
| 11 | #pod # [ call => [ var => 'UPPER' ], [ binop => '&', [ string => "hello " ], [ var => "name" ] ] ] | ||||||||||||||||||||||||||||||||
| 12 | #pod my $tree = $f->parse( 'UPPER( "hello " & name )' ); | ||||||||||||||||||||||||||||||||
| 13 | #pod | ||||||||||||||||||||||||||||||||
| 14 | #pod # HELLO LEELA | ||||||||||||||||||||||||||||||||
| 15 | #pod my $res = $f->eval( 'UPPER( "hello " & name )', { name => 'Leela' } ); | ||||||||||||||||||||||||||||||||
| 16 | #pod | ||||||||||||||||||||||||||||||||
| 17 | #pod # { greeting => "HELLO LEELA" } | ||||||||||||||||||||||||||||||||
| 18 | #pod my $data = $f->resolve( { greeting => 'UPPER( "hello " & name )' }, { name => 'Leela' } ); | ||||||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||||||
| 20 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||||
| 22 | #pod This module parses and evaluates formulas. Formulas are strings that | ||||||||||||||||||||||||||||||||
| 23 | #pod begin with one C<=> and contain an expression. Formula expressions can | ||||||||||||||||||||||||||||||||
| 24 | #pod contain strings, numbers, variables, binary operations, function calls, | ||||||||||||||||||||||||||||||||
| 25 | #pod and array or hash literals. | ||||||||||||||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||||||||||||||
| 27 | #pod =head1 FORMULA SYNTAX | ||||||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||||||
| 29 | #pod Where possible, the formula syntax resembles the syntax from popular spreadsheet | ||||||||||||||||||||||||||||||||
| 30 | #pod programs like Lotus 1-2-3, Excel, and Sheets, and Microsoft's Power Fx. | ||||||||||||||||||||||||||||||||
| 31 | #pod | ||||||||||||||||||||||||||||||||
| 32 | #pod =head2 Strings | ||||||||||||||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||||||||||||||
| 34 | #pod Strings are surrounded by double-quotes (C<"Professor Fisherprice | ||||||||||||||||||||||||||||||||
| 35 | #pod Shpeekenshpell">). Double-quotes can be inserted into strings by adding | ||||||||||||||||||||||||||||||||
| 36 | #pod a backslash (C<"The horse says \"Doctorate Denied\"">). | ||||||||||||||||||||||||||||||||
| 37 | #pod | ||||||||||||||||||||||||||||||||
| 38 | #pod =head2 Numbers | ||||||||||||||||||||||||||||||||
| 39 | #pod | ||||||||||||||||||||||||||||||||
| 40 | #pod Numbers can be integers (C<312>) or decimals (C<3.12>). Negative numbers have | ||||||||||||||||||||||||||||||||
| 41 | #pod C<-> in front (C<-3.12>). | ||||||||||||||||||||||||||||||||
| 42 | #pod | ||||||||||||||||||||||||||||||||
| 43 | #pod =head2 Variables | ||||||||||||||||||||||||||||||||
| 44 | #pod | ||||||||||||||||||||||||||||||||
| 45 | #pod Variables start with a letter and can contain letters, numbers, and | ||||||||||||||||||||||||||||||||
| 46 | #pod underscores (C<_>). Variable values are defined in the context. | ||||||||||||||||||||||||||||||||
| 47 | #pod Variables cannot be created by formulas (yet). | ||||||||||||||||||||||||||||||||
| 48 | #pod | ||||||||||||||||||||||||||||||||
| 49 | #pod =head2 Binary Operators | ||||||||||||||||||||||||||||||||
| 50 | #pod | ||||||||||||||||||||||||||||||||
| 51 | #pod =over | ||||||||||||||||||||||||||||||||
| 52 | #pod | ||||||||||||||||||||||||||||||||
| 53 | #pod =item Mathematical Operators | ||||||||||||||||||||||||||||||||
| 54 | #pod | ||||||||||||||||||||||||||||||||
| 55 | #pod # Addition | ||||||||||||||||||||||||||||||||
| 56 | #pod 1.2 + 3 -> 4.2 | ||||||||||||||||||||||||||||||||
| 57 | #pod | ||||||||||||||||||||||||||||||||
| 58 | #pod # Subtraction | ||||||||||||||||||||||||||||||||
| 59 | #pod 4 - 2.3 -> 1.7 | ||||||||||||||||||||||||||||||||
| 60 | #pod | ||||||||||||||||||||||||||||||||
| 61 | #pod # Multiplication | ||||||||||||||||||||||||||||||||
| 62 | #pod 2 * 3 -> 6 | ||||||||||||||||||||||||||||||||
| 63 | #pod | ||||||||||||||||||||||||||||||||
| 64 | #pod # Division | ||||||||||||||||||||||||||||||||
| 65 | #pod 8 / 2 -> 4 | ||||||||||||||||||||||||||||||||
| 66 | #pod | ||||||||||||||||||||||||||||||||
| 67 | #pod # Exponentation | ||||||||||||||||||||||||||||||||
| 68 | #pod 2 ^ 3 -> 8 | ||||||||||||||||||||||||||||||||
| 69 | #pod | ||||||||||||||||||||||||||||||||
| 70 | #pod =item String Operators | ||||||||||||||||||||||||||||||||
| 71 | #pod | ||||||||||||||||||||||||||||||||
| 72 | #pod # Concatenation | ||||||||||||||||||||||||||||||||
| 73 | #pod "Hello, " & "World" -> "Hello, World" | ||||||||||||||||||||||||||||||||
| 74 | #pod | ||||||||||||||||||||||||||||||||
| 75 | #pod =item Logical Operators | ||||||||||||||||||||||||||||||||
| 76 | #pod | ||||||||||||||||||||||||||||||||
| 77 | #pod # Equality | ||||||||||||||||||||||||||||||||
| 78 | #pod 2 = 2 -> TRUE | ||||||||||||||||||||||||||||||||
| 79 | #pod | ||||||||||||||||||||||||||||||||
| 80 | #pod # Inequality | ||||||||||||||||||||||||||||||||
| 81 | #pod 3 <> 3 -> FALSE | ||||||||||||||||||||||||||||||||
| 82 | #pod | ||||||||||||||||||||||||||||||||
| 83 | #pod # Less-/Greater-than | ||||||||||||||||||||||||||||||||
| 84 | #pod 3 < 8 -> TRUE | ||||||||||||||||||||||||||||||||
| 85 | #pod 3 > 8 -> FALSE | ||||||||||||||||||||||||||||||||
| 86 | #pod | ||||||||||||||||||||||||||||||||
| 87 | #pod # Less-/Greater-than-or-equal | ||||||||||||||||||||||||||||||||
| 88 | #pod 3 <= 2 -> FALSE | ||||||||||||||||||||||||||||||||
| 89 | #pod 3 >= 3 -> TRUE | ||||||||||||||||||||||||||||||||
| 90 | #pod | ||||||||||||||||||||||||||||||||
| 91 | #pod =back | ||||||||||||||||||||||||||||||||
| 92 | #pod | ||||||||||||||||||||||||||||||||
| 93 | #pod =head2 Function Calls | ||||||||||||||||||||||||||||||||
| 94 | #pod | ||||||||||||||||||||||||||||||||
| 95 | #pod Function calls start with the name of the function followed by empty parentheses or | ||||||||||||||||||||||||||||||||
| 96 | #pod parentheses containing function parameters (expressions) separated by commas. | ||||||||||||||||||||||||||||||||
| 97 | #pod | ||||||||||||||||||||||||||||||||
| 98 | #pod IF( name = "Leela", TRUE(), FALSE() ) | ||||||||||||||||||||||||||||||||
| 99 | #pod | ||||||||||||||||||||||||||||||||
| 100 | #pod See L</FUNCTIONS> for a list of available functions. | ||||||||||||||||||||||||||||||||
| 101 | #pod | ||||||||||||||||||||||||||||||||
| 102 | #pod =head2 Arrays | ||||||||||||||||||||||||||||||||
| 103 | #pod | ||||||||||||||||||||||||||||||||
| 104 | #pod Arrays begin with square brackets and contain expressions separated by commas. | ||||||||||||||||||||||||||||||||
| 105 | #pod | ||||||||||||||||||||||||||||||||
| 106 | #pod [ name, name = "Leela", TRUE() ] | ||||||||||||||||||||||||||||||||
| 107 | #pod | ||||||||||||||||||||||||||||||||
| 108 | #pod Get a value from an array using square brackets and the index of the | ||||||||||||||||||||||||||||||||
| 109 | #pod item (0-based). | ||||||||||||||||||||||||||||||||
| 110 | #pod | ||||||||||||||||||||||||||||||||
| 111 | #pod # Characters = [ "Fry", "Leela", "Bender" ] | ||||||||||||||||||||||||||||||||
| 112 | #pod Characters[2] # Bender | ||||||||||||||||||||||||||||||||
| 113 | #pod | ||||||||||||||||||||||||||||||||
| 114 | #pod =head2 Hashes | ||||||||||||||||||||||||||||||||
| 115 | #pod | ||||||||||||||||||||||||||||||||
| 116 | #pod Hashes begin with curly braces and contain key/value pairs separated by commas. | ||||||||||||||||||||||||||||||||
| 117 | #pod Keys must be strings (for now) and are separated from values with colons. | ||||||||||||||||||||||||||||||||
| 118 | #pod | ||||||||||||||||||||||||||||||||
| 119 | #pod { "name": "Leela", "captain": TRUE() } | ||||||||||||||||||||||||||||||||
| 120 | #pod | ||||||||||||||||||||||||||||||||
| 121 | #pod Get a value from a hash using dot followed by the key. | ||||||||||||||||||||||||||||||||
| 122 | #pod | ||||||||||||||||||||||||||||||||
| 123 | #pod # Employees = { "Pilot": "Leela", "Cook": "Bender", "Person": "Fry" } | ||||||||||||||||||||||||||||||||
| 124 | #pod Employees.Pilot # Leela | ||||||||||||||||||||||||||||||||
| 125 | #pod | ||||||||||||||||||||||||||||||||
| 126 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 127 | #pod | ||||||||||||||||||||||||||||||||
| 128 | #pod L<Zapp::Task>, L<Zapp> | ||||||||||||||||||||||||||||||||
| 129 | #pod | ||||||||||||||||||||||||||||||||
| 130 | #pod =cut | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 20 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 133 | 20 | ▶ | use Zapp::Util qw( get_path_from_data ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 134 | 20 | ▶ | use List::Util qw( any all pairs ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | # XXX: binops, functions, and grammar should all be attributes so that | ||||||||||||||||||||||||||||||||
| 137 | # they can be configured per-instance | ||||||||||||||||||||||||||||||||
| 138 | our %BINOPS = ( | ||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | '+' => sub { $_[0] + $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | '-' => sub { $_[0] - $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | '*' => sub { $_[0] * $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | '/' => sub { $_[0] / $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | '^' => sub { $_[0] ** $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 144 | 6 | ▶ | '&' => sub { $_[0] . $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 145 | # XXX: Logical binops need to detect numbers vs. strings and change | ||||||||||||||||||||||||||||||||
| 146 | # comparisons | ||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | '=' => sub { $_[0] eq $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | '>' => sub { $_[0] gt $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | '<' => sub { $_[0] lt $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | '>=' => sub { $_[0] ge $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | '<=' => sub { $_[0] le $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | '<>' => sub { $_[0] ne $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 153 | 20 | ); | |||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | ▶ | our %FUNCTIONS = ( | |||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 156 | ### Logic functions | ||||||||||||||||||||||||||||||||
| 157 | TRUE => \&_func_true, | ||||||||||||||||||||||||||||||||
| 158 | FALSE => \&_func_false, | ||||||||||||||||||||||||||||||||
| 159 | NOT => \&_func_not, | ||||||||||||||||||||||||||||||||
| 160 | IF => \&_func_if, | ||||||||||||||||||||||||||||||||
| 161 | IFS => \&_func_ifs, | ||||||||||||||||||||||||||||||||
| 162 | AND => \&_func_and, | ||||||||||||||||||||||||||||||||
| 163 | OR => \&_func_or, | ||||||||||||||||||||||||||||||||
| 164 | XOR => \&_func_xor, | ||||||||||||||||||||||||||||||||
| 165 | EVAL => \&_func_eval, | ||||||||||||||||||||||||||||||||
| 166 | ### Text functions | ||||||||||||||||||||||||||||||||
| 167 | # Case manipulation | ||||||||||||||||||||||||||||||||
| 168 | 1 | LOWER => sub( $f, $str ) { lc $str }, | |||||||||||||||||||||||||||||||
| 169 | 4 | ▶ | UPPER => sub( $f, $str ) { uc $str }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | PROPER => sub( $f, $str ) { ( lc $str ) =~ s/(?:^|[^a-zA-Z'])([a-z])/uc $1/er }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 171 | # Substrings | ||||||||||||||||||||||||||||||||
| 172 | 2 | ▶ | LEFT => sub( $f, $str, $len ) { substr $str, 0, $len }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | RIGHT => sub( $f, $str, $len ) { substr $str, -$len }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 174 | 20 | ); | |||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 20 | my ( @result, @term, @args, @binop, @call, @array, @hash, @var, $depth, $expected, $failed_at ); | |||||||||||||||||||||||||||||||
| 177 | our $GRAMMAR = qr{ | ||||||||||||||||||||||||||||||||
| 178 | (?(DEFINE) | ||||||||||||||||||||||||||||||||
| 179 | (?<EXPR> | ||||||||||||||||||||||||||||||||
| 180 | # Expressions can recurse, so we need to use a stack. When | ||||||||||||||||||||||||||||||||
| 181 | # we recurse, we must take the result off the stack and save | ||||||||||||||||||||||||||||||||
| 182 | # it until we can put it back on the stack (somewhere) | ||||||||||||||||||||||||||||||||
| 183 | (?{ $depth++ })(?> | ||||||||||||||||||||||||||||||||
| 184 | \s* | ||||||||||||||||||||||||||||||||
| 185 | (?: | ||||||||||||||||||||||||||||||||
| 186 | # Terminator first, to escape infinite loops | ||||||||||||||||||||||||||||||||
| 187 | (?> (?&TERM) ) (?! (?&OP) | \( ) \s* | ||||||||||||||||||||||||||||||||
| 188 | (*COMMIT) (?{ push @result, pop @term }) | ||||||||||||||||||||||||||||||||
| 189 | # If there is more to match, it must've been an attempt | ||||||||||||||||||||||||||||||||
| 190 | # at an operator | ||||||||||||||||||||||||||||||||
| 191 | (?{ $expected = 'Expected operator'; $failed_at = pos() }) | ||||||||||||||||||||||||||||||||
| 192 | | | ||||||||||||||||||||||||||||||||
| 193 | (?> (?&CALL) ) (?! (?&OP) ) \s* | ||||||||||||||||||||||||||||||||
| 194 | (*COMMIT) (?{ push @result, [ call => @{ pop @call } ] }) | ||||||||||||||||||||||||||||||||
| 195 | # If there is more to match, it must've been an attempt | ||||||||||||||||||||||||||||||||
| 196 | # at an operator | ||||||||||||||||||||||||||||||||
| 197 | (?{ $expected = 'Expected operator'; $failed_at = pos() }) | ||||||||||||||||||||||||||||||||
| 198 | | | ||||||||||||||||||||||||||||||||
| 199 | (?> (?&ARRAY) ) (?! (?&OP) ) | ||||||||||||||||||||||||||||||||
| 200 | (*COMMIT) (?{ push @result, [ array => @{ pop @array } ] }) | ||||||||||||||||||||||||||||||||
| 201 | | | ||||||||||||||||||||||||||||||||
| 202 | (?> (?&HASH) ) (?! (?&OP) ) | ||||||||||||||||||||||||||||||||
| 203 | (*COMMIT) (?{ push @result, [ hash => @{ pop @hash } ] }) | ||||||||||||||||||||||||||||||||
| 204 | | | ||||||||||||||||||||||||||||||||
| 205 | (?{ push @binop, [] }) | ||||||||||||||||||||||||||||||||
| 206 | (?> | ||||||||||||||||||||||||||||||||
| 207 | (?> (?&CALL) ) | ||||||||||||||||||||||||||||||||
| 208 | (?{ push @{ $binop[-1] }, [ call => @{ pop @call } ] }) | ||||||||||||||||||||||||||||||||
| 209 | | | ||||||||||||||||||||||||||||||||
| 210 | (?> (?&TERM) ) | ||||||||||||||||||||||||||||||||
| 211 | (?{ push @{ $binop[-1] }, [ @{ pop @term } ] }) | ||||||||||||||||||||||||||||||||
| 212 | ) | ||||||||||||||||||||||||||||||||
| 213 | (*COMMIT) (?{ $expected = 'Expected operator'; $failed_at = pos() }) | ||||||||||||||||||||||||||||||||
| 214 | (?<op> (?&OP) ) \s* | ||||||||||||||||||||||||||||||||
| 215 | (*COMMIT) (?{ $expected = 'Expected expression'; $failed_at = pos() }) | ||||||||||||||||||||||||||||||||
| 216 | (?> (?&EXPR) ) | ||||||||||||||||||||||||||||||||
| 217 | (?{ push @result, [ binop => $+{op}, @{ pop @binop }, pop @result ] }) | ||||||||||||||||||||||||||||||||
| 218 | | | ||||||||||||||||||||||||||||||||
| 219 | # Characters that cannot be used to start a term, call, | ||||||||||||||||||||||||||||||||
| 220 | # array, or hash | ||||||||||||||||||||||||||||||||
| 221 | [^a-zA-Z0-9\."\-\[\{] | ||||||||||||||||||||||||||||||||
| 222 | (*FAIL) | ||||||||||||||||||||||||||||||||
| 223 | ) | ||||||||||||||||||||||||||||||||
| 224 | )(?{ $depth-- }) | ||||||||||||||||||||||||||||||||
| 225 | ) | ||||||||||||||||||||||||||||||||
| 226 | (?<OP>(?> @{[ join '|', map quotemeta, keys %BINOPS ]} )) | ||||||||||||||||||||||||||||||||
| 227 | (?<CALL>(?> | ||||||||||||||||||||||||||||||||
| 228 | (?&VAR) | ||||||||||||||||||||||||||||||||
| 229 | (?{ push @call, [ [ var => @var ] ]; @var = () }) | ||||||||||||||||||||||||||||||||
| 230 | \s* \( \s* | ||||||||||||||||||||||||||||||||
| 231 | (?> | ||||||||||||||||||||||||||||||||
| 232 | (?{ push @args, [] }) | ||||||||||||||||||||||||||||||||
| 233 | (?> | ||||||||||||||||||||||||||||||||
| 234 | (?&EXPR) | ||||||||||||||||||||||||||||||||
| 235 | (?{ push @{ $args[-1] }, pop @result }) | ||||||||||||||||||||||||||||||||
| 236 | )? | ||||||||||||||||||||||||||||||||
| 237 | (?: | ||||||||||||||||||||||||||||||||
| 238 | \s* , \s* (?> (?&EXPR) ) | ||||||||||||||||||||||||||||||||
| 239 | (?{ push @{ $args[-1] }, pop @result }) | ||||||||||||||||||||||||||||||||
| 240 | )* | ||||||||||||||||||||||||||||||||
| 241 | ) | ||||||||||||||||||||||||||||||||
| 242 | \s* (*COMMIT) | ||||||||||||||||||||||||||||||||
| 243 | (?{ $expected = 'Could not find end parenthesis'; $failed_at = pos() }) | ||||||||||||||||||||||||||||||||
| 244 | \) \s* | ||||||||||||||||||||||||||||||||
| 245 | (?{ push $call[-1]->@*, @{ pop @args } }) | ||||||||||||||||||||||||||||||||
| 246 | )) | ||||||||||||||||||||||||||||||||
| 247 | (?<ARRAY>(?> | ||||||||||||||||||||||||||||||||
| 248 | \[ \s* | ||||||||||||||||||||||||||||||||
| 249 | (?{ push @array, [] }) | ||||||||||||||||||||||||||||||||
| 250 | (?: | ||||||||||||||||||||||||||||||||
| 251 | (?> (?&EXPR) ) \s* ,? \s* | ||||||||||||||||||||||||||||||||
| 252 | (?{ push @{ $array[-1] }, pop @result }) | ||||||||||||||||||||||||||||||||
| 253 | )* | ||||||||||||||||||||||||||||||||
| 254 | \] \s* | ||||||||||||||||||||||||||||||||
| 255 | )) | ||||||||||||||||||||||||||||||||
| 256 | (?<HASH>(?> | ||||||||||||||||||||||||||||||||
| 257 | \{ \s* | ||||||||||||||||||||||||||||||||
| 258 | (?{ push @hash, [] }) | ||||||||||||||||||||||||||||||||
| 259 | (?: | ||||||||||||||||||||||||||||||||
| 260 | (?> (?<key> (?&STRING) ) ) \s* : \s* | ||||||||||||||||||||||||||||||||
| 261 | (?> (?&EXPR) ) \s* ,? \s* | ||||||||||||||||||||||||||||||||
| 262 | (?{ push @{ $hash[-1] }, [ $+{'key'}, pop @result ] }) | ||||||||||||||||||||||||||||||||
| 263 | )* | ||||||||||||||||||||||||||||||||
| 264 | \} \s* | ||||||||||||||||||||||||||||||||
| 265 | )) | ||||||||||||||||||||||||||||||||
| 266 | (?<TERM>(?> | ||||||||||||||||||||||||||||||||
| 267 | (?: | ||||||||||||||||||||||||||||||||
| 268 | (?<string> (?&STRING) ) | ||||||||||||||||||||||||||||||||
| 269 | (?{ push @term, [ %+{'string'} ] }) | ||||||||||||||||||||||||||||||||
| 270 | | | ||||||||||||||||||||||||||||||||
| 271 | (?<number> (?&NUMBER) ) | ||||||||||||||||||||||||||||||||
| 272 | (?{ push @term, [ %+{'number'} ] }) | ||||||||||||||||||||||||||||||||
| 273 | | | ||||||||||||||||||||||||||||||||
| 274 | (?&VAR) | ||||||||||||||||||||||||||||||||
| 275 | (?{ push @term, [ var => @var ]; @var = () }) | ||||||||||||||||||||||||||||||||
| 276 | ) | ||||||||||||||||||||||||||||||||
| 277 | \s* | ||||||||||||||||||||||||||||||||
| 278 | )) | ||||||||||||||||||||||||||||||||
| 279 | (?<VAR> | ||||||||||||||||||||||||||||||||
| 280 | (?<word> [a-zA-Z][a-zA-Z0-9_]+ ) \s* | ||||||||||||||||||||||||||||||||
| 281 | (?{ push @var, $+{word} }) | ||||||||||||||||||||||||||||||||
| 282 | (?: \. \s* (?&VAR) )*+ | ||||||||||||||||||||||||||||||||
| 283 | ) | ||||||||||||||||||||||||||||||||
| 284 | (?<STRING> | ||||||||||||||||||||||||||||||||
| 285 | " | ||||||||||||||||||||||||||||||||
| 286 | (?> | ||||||||||||||||||||||||||||||||
| 287 | [^"\\]*+ (?: \\" [^"\\]*+ )*+ | ||||||||||||||||||||||||||||||||
| 288 | ) | ||||||||||||||||||||||||||||||||
| 289 | (*COMMIT) (?{ $expected = 'Could not find closing quote for string'; $failed_at = pos() }) | ||||||||||||||||||||||||||||||||
| 290 | " | ||||||||||||||||||||||||||||||||
| 291 | ) | ||||||||||||||||||||||||||||||||
| 292 | (?<NUMBER> -? \d+ %? | -? \d* \. \d+ %? ) | ||||||||||||||||||||||||||||||||
| 293 | ) | ||||||||||||||||||||||||||||||||
| 294 | 20 | }xms; | |||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | has _context => sub { {} }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | # XXX: Strings that look like money amounts can be coerced into numbers | ||||||||||||||||||||||||||||||||
| 299 | # XXX: Strings that look like dates can be coerced into dates | ||||||||||||||||||||||||||||||||
| 300 | # ... Or maybe not, since that's one of the biggest complaints | ||||||||||||||||||||||||||||||||
| 301 | # about Excel. Though, that might just refer to the | ||||||||||||||||||||||||||||||||
| 302 | # auto-formatting thing, which we will not be doing. | ||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||
| 304 | #pod =method parse | ||||||||||||||||||||||||||||||||
| 305 | #pod | ||||||||||||||||||||||||||||||||
| 306 | #pod my $tree = $f->parse( $formula ) | ||||||||||||||||||||||||||||||||
| 307 | #pod | ||||||||||||||||||||||||||||||||
| 308 | #pod Parse the given formula (without C<=> prefix) and return an abstract | ||||||||||||||||||||||||||||||||
| 309 | #pod syntax tree that can be evaluated. | ||||||||||||||||||||||||||||||||
| 310 | #pod | ||||||||||||||||||||||||||||||||
| 311 | #pod =cut | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | # Does not expect `=` prefix | ||||||||||||||||||||||||||||||||
| 314 | ▶ | sub parse( $self, $expr ) { | |||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 315 | 57 | @result = (); | |||||||||||||||||||||||||||||||
| 316 | 57 | $depth = 0; | |||||||||||||||||||||||||||||||
| 317 | 57 | $expected = ''; | |||||||||||||||||||||||||||||||
| 318 | 57 | $failed_at = 0; | |||||||||||||||||||||||||||||||
| 319 | 57 | ▶ | unless ( $expr =~ /${GRAMMAR}^(?&EXPR)$/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 320 | # XXX: Parse error handling. DCONWAY has numerous | ||||||||||||||||||||||||||||||||
| 321 | # (?{ $expected = '...'; $failed_at = pos() }) in his | ||||||||||||||||||||||||||||||||
| 322 | # Keyword::Declare grammar. If parsing stops, the last value in | ||||||||||||||||||||||||||||||||
| 323 | # those vars is used to show an error message. | ||||||||||||||||||||||||||||||||
| 324 | 4 | ▶ | $failed_at = 'end of input' if $failed_at >= length $expr; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 325 | 4 | die "Syntax error: $expected at $failed_at.\n"; | |||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | 53 | return $result[0]; | |||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||
| 330 | #pod =method eval | ||||||||||||||||||||||||||||||||
| 331 | #pod | ||||||||||||||||||||||||||||||||
| 332 | #pod my $value = $f->eval( $formula, $context ); | ||||||||||||||||||||||||||||||||
| 333 | #pod | ||||||||||||||||||||||||||||||||
| 334 | #pod Parse and execute the given formula (without C<=> prefix) using the given context as | ||||||||||||||||||||||||||||||||
| 335 | #pod values for any variables. | ||||||||||||||||||||||||||||||||
| 336 | #pod | ||||||||||||||||||||||||||||||||
| 337 | #pod =cut | ||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | # Does not expect `=` prefix | ||||||||||||||||||||||||||||||||
| 340 | 36 | ▶ | sub eval( $self, $expr, $context={} ) { | ||||||||||||||||||||||||||||||
|
Subroutine
eval: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 341 | 36 | $self->_context( $context ); | |||||||||||||||||||||||||||||||
| 342 | 36 | my $tree = $self->parse( $expr ); | |||||||||||||||||||||||||||||||
| 343 | ▶ | my $handle = sub( $tree ) { | |||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 344 | 96 | ▶ | if ( $tree->[0] eq 'string' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 345 | # XXX: strip slashes | ||||||||||||||||||||||||||||||||
| 346 | 26 | my $string = substr $tree->[1], 1, -1; | |||||||||||||||||||||||||||||||
| 347 | 26 | $string =~ s/\\(?!\\)//g; | |||||||||||||||||||||||||||||||
| 348 | 26 | return $string; | |||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||
| 350 | 70 | ▶ | if ( $tree->[0] eq 'number' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 351 | 2 | return $tree->[1]; | |||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||
| 353 | 68 | ▶ | if ( $tree->[0] eq 'var' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 354 | 9 | my $var = join '.', $tree->@[1..$#$tree]; | |||||||||||||||||||||||||||||||
| 355 | 9 | my $context = $self->_context; | |||||||||||||||||||||||||||||||
| 356 | 9 | ▶ | return ref $context eq 'CODE' ? $context->( $var ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 357 | : get_path_from_data( $var, $context ) | ||||||||||||||||||||||||||||||||
| 358 | ; | ||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||
| 360 | 59 | ▶ | if ( $tree->[0] eq 'call' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 361 | 53 | my $name = join '.', $tree->[1]->@[1..$tree->[1]->$#*]; | |||||||||||||||||||||||||||||||
| 362 | 53 | my @args = map { __SUB__->( $_ ) } @{$tree}[2 .. $#{$tree}]; | |||||||||||||||||||||||||||||||
| 363 | 53 | return $FUNCTIONS{ $name }->( $self, @args ); | |||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||
| 365 | 6 | ▶ | if ( $tree->[0] eq 'binop' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 366 | 6 | my $op = $tree->[1]; | |||||||||||||||||||||||||||||||
| 367 | 6 | my $left = __SUB__->( $tree->[2] ); | |||||||||||||||||||||||||||||||
| 368 | 6 | my $right = __SUB__->( $tree->[3] ); | |||||||||||||||||||||||||||||||
| 369 | 6 | return $BINOPS{ $op }->( $left, $right ); | |||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||
| 371 | 0 | die "Unknown parse result: $tree->[0]"; | |||||||||||||||||||||||||||||||
| 372 | 36 | }; | |||||||||||||||||||||||||||||||
| 373 | 36 | my $result = $handle->( $tree ); | |||||||||||||||||||||||||||||||
| 374 | 36 | return $result; | |||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||
| 377 | #pod =method resolve | ||||||||||||||||||||||||||||||||
| 378 | #pod | ||||||||||||||||||||||||||||||||
| 379 | #pod my $data = $f->resolve( $data, $context ); | ||||||||||||||||||||||||||||||||
| 380 | #pod | ||||||||||||||||||||||||||||||||
| 381 | #pod Resolve all formulas in the data structure C<$data> and return a new data structure | ||||||||||||||||||||||||||||||||
| 382 | #pod with the resolved values. Formulas are strings that begin with C<=>. Use C<==> to escape | ||||||||||||||||||||||||||||||||
| 383 | #pod parsing. | ||||||||||||||||||||||||||||||||
| 384 | #pod | ||||||||||||||||||||||||||||||||
| 385 | #pod # { val => 1, str => '=num' } | ||||||||||||||||||||||||||||||||
| 386 | #pod $f->resolve( { val => '=num', str => '==num' }, { num => 1 } ); | ||||||||||||||||||||||||||||||||
| 387 | #pod | ||||||||||||||||||||||||||||||||
| 388 | #pod =cut | ||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||
| 390 | 9 | ▶ | sub resolve( $self, $data, $context={} ) { | ||||||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 391 | 4 | ▶ | return ref $data eq 'ARRAY' ? [ map { $self->resolve( $_, $context ) } @$data ] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 392 | 9 | ▶ | : ref $data eq 'HASH' ? { map { $_ => $self->resolve( $data->{ $_ }, $context ) } keys %$data } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
not ref $data and $data =~ /^=(?!=)/u
MC/DC50%1/2
not ref $data and $data =~ /^=(?!=)/u
not ref $data
$data =~ /^=(?!=)/u
Truth table66%2/3
not(A) and B
A= not ref $dataB= $data =~ /^=(?!=)/u
| |||||||||||||||||||||||||||||||||
| 393 | : !ref $data && $data =~ /^=(?!=)/ ? $self->eval( substr( $data, 1 ), $context ) | ||||||||||||||||||||||||||||||||
| 394 | : $data =~ s/^==/=/r; | ||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | #pod =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||
| 398 | #pod | ||||||||||||||||||||||||||||||||
| 399 | #pod =cut | ||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | # XXX: Add real-world examples of usage of all functions | ||||||||||||||||||||||||||||||||
| 402 | # NOTE: Arrange all functions in alphabetical order inside their | ||||||||||||||||||||||||||||||||
| 403 | # category | ||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | #pod =head2 Logic/Control Functions | ||||||||||||||||||||||||||||||||
| 406 | #pod | ||||||||||||||||||||||||||||||||
| 407 | #pod =head3 AND | ||||||||||||||||||||||||||||||||
| 408 | #pod | ||||||||||||||||||||||||||||||||
| 409 | #pod =AND( <expression>... ) | ||||||||||||||||||||||||||||||||
| 410 | #pod | ||||||||||||||||||||||||||||||||
| 411 | #pod Returns C<TRUE> if all expressions are true. | ||||||||||||||||||||||||||||||||
| 412 | #pod | ||||||||||||||||||||||||||||||||
| 413 | #pod =cut | ||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||
| 415 | ▶ | sub _func_and( $f, @exprs ) { | |||||||||||||||||||||||||||||||
|
Subroutine
_func_and: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 416 | 2 | ▶ | return ( all { !!$_ } @exprs ) ? _func_true($f) : _func_false($f); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||
| 419 | #pod =head3 EVAL | ||||||||||||||||||||||||||||||||
| 420 | #pod | ||||||||||||||||||||||||||||||||
| 421 | #pod =EVAL( <string> ) | ||||||||||||||||||||||||||||||||
| 422 | #pod | ||||||||||||||||||||||||||||||||
| 423 | #pod Evaluate the string as a formula and return the result. The string must | ||||||||||||||||||||||||||||||||
| 424 | #pod not begin with an C<=>. | ||||||||||||||||||||||||||||||||
| 425 | #pod | ||||||||||||||||||||||||||||||||
| 426 | #pod =cut | ||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||
| 428 | ▶ | sub _func_eval( $f, $expr ) { | |||||||||||||||||||||||||||||||
|
Subroutine
_func_eval: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 429 | # XXX: This context attribute is a bad way of doing things, but we | ||||||||||||||||||||||||||||||||
| 430 | # need some way for functions to get the context, or values from the | ||||||||||||||||||||||||||||||||
| 431 | # context... | ||||||||||||||||||||||||||||||||
| 432 | 2 | return $f->eval( $expr, $f->_context ); | |||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||
| 435 | #pod =head3 FALSE | ||||||||||||||||||||||||||||||||
| 436 | #pod | ||||||||||||||||||||||||||||||||
| 437 | #pod =FALSE() | ||||||||||||||||||||||||||||||||
| 438 | #pod | ||||||||||||||||||||||||||||||||
| 439 | #pod Returns a false value. | ||||||||||||||||||||||||||||||||
| 440 | #pod | ||||||||||||||||||||||||||||||||
| 441 | #pod =cut | ||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||
| 443 | ▶ | sub _func_false( $f ) { | |||||||||||||||||||||||||||||||
|
Subroutine
_func_false: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 444 | 18 | return Mojo::JSON->false; | |||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||
| 447 | #pod =head3 IF | ||||||||||||||||||||||||||||||||
| 448 | #pod | ||||||||||||||||||||||||||||||||
| 449 | #pod =IF( <expression>, <true_result>, <false_result> ) | ||||||||||||||||||||||||||||||||
| 450 | #pod | ||||||||||||||||||||||||||||||||
| 451 | #pod Evaluate the expression in C<expression> and return C<true_result> if | ||||||||||||||||||||||||||||||||
| 452 | #pod the condition is true, or C<false_result> if the condition is false. | ||||||||||||||||||||||||||||||||
| 453 | #pod | ||||||||||||||||||||||||||||||||
| 454 | #pod =cut | ||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | ▶ | sub _func_if( $f, $expr, $true_result, $false_result ) { | |||||||||||||||||||||||||||||||
|
Subroutine
_func_if: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 457 | 2 | ▶ | return $expr ? $true_result : $false_result; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||
| 460 | #pod =head3 IFS | ||||||||||||||||||||||||||||||||
| 461 | #pod | ||||||||||||||||||||||||||||||||
| 462 | #pod =IFS( <expression>, <result>, ..., <default_result> ) | ||||||||||||||||||||||||||||||||
| 463 | #pod | ||||||||||||||||||||||||||||||||
| 464 | #pod Evaluate each expression and return its corresponding result if the | ||||||||||||||||||||||||||||||||
| 465 | #pod expression is true. Return C<default_result> if no condition is true. | ||||||||||||||||||||||||||||||||
| 466 | #pod | ||||||||||||||||||||||||||||||||
| 467 | #pod =cut | ||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||
| 469 | ▶ | sub _func_ifs( $f, @args ) { | |||||||||||||||||||||||||||||||
|
Subroutine
_func_ifs: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 470 | 3 | my $default = pop @args; | |||||||||||||||||||||||||||||||
| 471 | 3 | for my $pair ( pairs @args ) { | |||||||||||||||||||||||||||||||
| 472 | 5 | ▶ | return $pair->[1] if $pair->[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||
| 474 | 1 | return $default; | |||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||
| 477 | #pod =head3 NOT | ||||||||||||||||||||||||||||||||
| 478 | #pod | ||||||||||||||||||||||||||||||||
| 479 | #pod =NOT( <expression> ) | ||||||||||||||||||||||||||||||||
| 480 | #pod | ||||||||||||||||||||||||||||||||
| 481 | #pod Returns C<TRUE> if the expression is true, C<FALSE> otherwise. | ||||||||||||||||||||||||||||||||
| 482 | #pod | ||||||||||||||||||||||||||||||||
| 483 | #pod =cut | ||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||
| 485 | ▶ | sub _func_not( $f, $expr ) { | |||||||||||||||||||||||||||||||
|
Subroutine
_func_not: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 486 | 2 | ▶ | return !!$expr ? _func_false($f) : _func_true($f); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||
| 489 | #pod =head3 OR | ||||||||||||||||||||||||||||||||
| 490 | #pod | ||||||||||||||||||||||||||||||||
| 491 | #pod =OR( <expression>... ) | ||||||||||||||||||||||||||||||||
| 492 | #pod | ||||||||||||||||||||||||||||||||
| 493 | #pod Returns C<TRUE> if one expression is true. | ||||||||||||||||||||||||||||||||
| 494 | #pod | ||||||||||||||||||||||||||||||||
| 495 | #pod =cut | ||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||
| 497 | ▶ | sub _func_or( $f, @exprs ) { | |||||||||||||||||||||||||||||||
|
Subroutine
_func_or: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 498 | 4 | ▶ | return ( any { !!$_ } @exprs ) ? _func_true($f) : _func_false($f); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||
| 501 | #pod =head3 TRUE | ||||||||||||||||||||||||||||||||
| 502 | #pod | ||||||||||||||||||||||||||||||||
| 503 | #pod =TRUE() | ||||||||||||||||||||||||||||||||
| 504 | #pod | ||||||||||||||||||||||||||||||||
| 505 | #pod Returns a true value. | ||||||||||||||||||||||||||||||||
| 506 | #pod | ||||||||||||||||||||||||||||||||
| 507 | #pod =cut | ||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||
| 509 | ▶ | sub _func_true( $f ) { | |||||||||||||||||||||||||||||||
|
Subroutine
_func_true: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 510 | 21 | return Mojo::JSON->true; | |||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||
| 513 | #pod =head3 XOR | ||||||||||||||||||||||||||||||||
| 514 | #pod | ||||||||||||||||||||||||||||||||
| 515 | #pod =XOR( <expression>... ) | ||||||||||||||||||||||||||||||||
| 516 | #pod | ||||||||||||||||||||||||||||||||
| 517 | #pod Returns C<TRUE> if one and only one expression is true. | ||||||||||||||||||||||||||||||||
| 518 | #pod | ||||||||||||||||||||||||||||||||
| 519 | #pod =cut | ||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||
| 521 | ▶ | sub _func_xor( $f, @exprs ) { | |||||||||||||||||||||||||||||||
|
Subroutine
_func_xor: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 522 | 2 | ▶ | return ( grep { !!$_ } @exprs ) == 1 ? _func_true($f) : _func_false($f); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||
| 525 | #=head2 Text Functions | ||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||
| 527 | #=cut | ||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||
| 529 | #=head2 Date/Time Functions | ||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||
| 531 | #=cut | ||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||
| 533 | 20 | 1; | |||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||
| 535 | __END__ | ||||||||||||||||||||||||||||||||