| 1 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package YAML::Logic; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Template; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Safe; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | our $VERSION = "0.05"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | our %OPS = map { $_ => 1 } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | qw(eq ne lt gt < > <= >= == =~ like); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 67 | ▶ | my($class, %options) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 67 | my $self = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | safe => Safe->new(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | template => Template->new(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | error => "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | %options, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 67 | $self->{safe}->permit(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 67 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | sub interpolate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 259 | ▶ | my($self, $input, $vars) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
interpolate: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 259 | ▶ | if(ref($input) eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # When working on the original, we got weird memory errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # in perl5.8, so just copy the hash. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 34 | my %dupe = %$input; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 34 | my @keyvals = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 34 | for my $entry (each %dupe) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 68 | push @keyvals, $self->interpolate( $entry, $vars ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 34 | return { @keyvals }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 225 | my $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 225 | $input =~ s/(?:\${([\w.]+)})/[%- $1 %]/gx; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 225 | $input =~ s/(?:\$([\w.]+)) /[%- $1 %]/gx; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | $self->{template}->process( \$input, $vars, \$out ) or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 225 | ▶ | LOGDIE $self->{template}->error(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 225 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | sub evaluate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 91 | ▶ | my($self, $data, $vars, $not_glob, $boolean_or) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=16 SCAR=27.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 91 | ▶ | $not_glob = 0 unless defined $not_glob; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 91 | ▶ | $boolean_or = 0 unless defined $boolean_or; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | DEBUG sub { "evaluate: " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | Dumper( $data ) . "\n" . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | Dumper( $vars ) . "\n" . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | "not_glob=$not_glob " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | "boolean_or=$boolean_or " . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 91 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 91 | ▶ | if( ref($data) eq "ARRAY" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 91 | my @data = @$data; # make a copy, so splice() doesn't destroy | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # the original. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 91 | ▶ | while( my($field, $value) = splice @data, 0, 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 115 | my $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | my $not; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 115 | ▶ | if(! defined $field) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | LOGDIE "Rule variable not defined (value=$value)", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | "(maybe YAML rule: !\$var without quotes?"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 115 | ▶ | if($field =~ s/^!//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 9 | $not = !$not_glob; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 115 | ▶ | if($field eq "or") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 18 | $res = $self->evaluate($value, $vars, $not, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } elsif( $field eq "and") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 8 | $res = $self->evaluate($value, $vars, $not); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 89 | $field = $self->interpolate($field, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 89 | $value = $self->interpolate($value, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 89 | ▶ | if(ref($value) eq "") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 55 | $res = $self->evaluate_single( $field, $value, "eq", $not ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } elsif(ref($value) eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 34 | my($op) = keys %$value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 34 | ($value) = values %$value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 34 | $res = $self->evaluate_single( $field, $value, $op, $not ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 114 | ▶ | if($boolean_or and $res) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$boolean_or and $res
MC/DC100%2/2
$boolean_or and $res
$boolean_or
$res
Truth table100%3/3
A and B
A= $boolean_orB= $res
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # It's a boolean OR, so all it takes is one true result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 12 | my $rc = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 12 | DEBUG "evaluate: rc=$rc"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 12 | return $rc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | ▶ | } elsif(!$boolean_or and !$res) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$boolean_or || $res
MC/DC100%2/2
$boolean_or || $res
$boolean_or
$res
Truth table100%3/3
A || B
A= $boolean_orB= $res
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # It's a boolean AND, so all it takes is one false result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 24 | my $rc = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 24 | DEBUG "evaluate: rc=$rc"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 24 | return $rc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | LOGDIE "Unknown type: $data"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # Return 1 if all ANDed conditions succeeded, and 0 if all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | # ORed conditions failed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 54 | my $rc = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 54 | ▶ | $rc = 0 if $boolean_or; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 54 | DEBUG "evaluate: rc=$rc"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 54 | return $rc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | sub evaluate_single { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 89 | ▶ | my($self, $field, $value, $op, $not) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate_single: called CC=13 SCAR=25.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 89 | $op = lc $op ; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 89 | ▶ | $op = '=~' if $op eq "like"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 89 | $self->error(""); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 89 | ▶ | if(! exists $OPS{ $op }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | LOGDIE "Unknown op: $op"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 89 | $field = '"' . esc($field) . '"'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 89 | my $cmd; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 89 | ▶ | if($op eq "=~") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 20 | ▶ | if($value =~ /\?\{/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | LOGDIE "Trapped ?{ in regex."; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 19 | $value =~ s#(\\\\|/)#\\$1#g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # If we ever get something like \\/, slap another backslash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # onto the "/" to mask it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 19 | ▶ | $value =~ s#(\\+)/# (length($1) % 2) ? "$1/" : "$1\\/"#ge; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 19 | $value = qr($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 19 | $cmd = "$field =~ /$value/"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 69 | $value = '"' . esc($value) . '"'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 69 | $cmd = "$field $op $value"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 88 | INFO "Test: $cmd"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 88 | my $res = $self->{safe}->reval($cmd); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 88 | ▶ | if($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | LOGDIE "Evaling [$cmd] failed: $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 88 | ▶ | if(!$res and !$not or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
not $res || $not or $res and $not
$res || $not
$res and $not
MC/DC50%2/4
not $res || $not or $res and $not
$res
$not
$res
$not
Truth table42%3/7
not(A || B) or C and D
A= $resB= $notC= $resD= $not
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | $res and $not) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 35 | ▶ | $res = "" if !defined $res; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 35 | $self->error("Test [$cmd] returned [$res]"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 88 | ▶ | return ($not ? (!$res) : $res); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | sub error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | ########################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 127 | ▶ | my($self, $error) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 127 | ▶ | if(defined $error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 124 | $self->{error} = $error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 127 | return $self->{error}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | ############################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | sub esc { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | ############################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 158 | ▶ | my($str, $metas) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
esc: called CC=2 SCAR=7.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 158 | $str =~ s/([\\"])/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 158 | ▶ | if(defined $metas) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $metas =~ s/\]/\\]/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $str =~ s/([$metas])/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 158 | return $str; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | YAML::Logic - Simple boolean logic in YAML | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | use YAML::Syck qw(Load); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | use YAML::Logic; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | my $logic = YAML::Logic->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | ### Tests defined somewhere in a YAML file ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | my $data = Load(q{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # is $var equal to "foo"? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | ### Tests performed in application code: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | if( $logic->evaluate( $data->{rule}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | { var => "foo" }) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | print "True!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | YAML::Logic allows users to define simple boolean logic in a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | configuration file, without permitting them to run arbitrary code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | While Perl code can be controlled with the C<Safe> module, C<Safe> can't | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | prevent the user from defining infinite loops, exhausting all available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | memory or crashing the interpreter by exploiting well-known perl bugs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | YAML::Logic isn't perfect in this regard either, but it makes it reasonably | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | hard to define harmful code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | The syntax for the boolean logic within a YAML file was inspired by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | John Siracusa's C<Rose::DB::Object::QueryBuilder> module, which provides | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | data structures to define logic that is then transformed into SQL. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | YAML::Logic takes the data structure instead and transforms it into Perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | For example, the data structure to check whether a variable C<$var> is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | equal to a value "foo", looks like this: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | [$var, "foo"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | It's a reference to an array containing both the value of the variable and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | the value to compare it with. In YAML, this looks like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | and this is exactly the syntax that YAML::Logic accepts. Note that after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | parsing the YAML configuration above, you need to pass I<only> the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | array ref inside the C<rule> entry to YAML::Logic's C<evaluate()> method: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | $logic->evaluate( $yaml_data->{rule}, ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | Passing the entire YAML data would cause an error with YAML::Logic, as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | it expects to receive an array ref. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | =head2 Variable Interpolation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | Note that variables like C<$var> will be interpolated so that they'll | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | represent their value before the evaluation starts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | So if you have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | and run | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | my $data = YAML::Load( $yaml ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | my $rc = $logic->evaluate( $data->{rule}, { var => "bar" } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | then YAML::Logic will substitute C<$var> by the string "bar", and then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | run the test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | ["bar", "foo"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | which checks if "bar" equals "foo". Since this is false, C<evaluate> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | returns false. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | Variable interpolation happens on both sides of the comparison, so comparing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | - $foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | - $bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | will search for two variables, C<foo> and C<bar>, replace the placeholders | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | by the variable values and then compare the two entries. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | If a referenced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | variable is immediately followed by some text, you can also use Perl's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | C<${foo}> notation to reference a variable: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | - ${foo}text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | - $bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | Several comparisons can be combined by lining them up in the array. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | The lineup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | [$var1, "foo", $var2, "bar"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | returns true if $var1 is equal to "foo" I<and> $var2 is equal to "bar". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | In YAML::Logic syntax, these two ANDed comparisons are written as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | - $var1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | - $var2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | - bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | in a YAML file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | Interpolation is done by the C<Template Toolkit>, so all the magic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | it does for arrays and hashes applies: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | - $hash.somekey | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | my $data = YAML::Load( $yaml ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | my $rc = $logic->evaluate( $data->{rule}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | { hash => { somekey => "foo" } } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | will test if "foo" equals "foo" and hence return a true value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | Likewise, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | - $array.1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | - el2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | my $data = YAML::Load( $yaml ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | my $rc = $logic->evaluate( $data->{rule}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | { array => [ 'el1', 'el2' ] } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | will test if "el2" equals "el2" and return a true value. Check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | C<perldoc Template> or read the O'Reilly Template Toolkit book for a more | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | detailed explanation of Template's variable interpolation magic. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | =head2 Other Comparators | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | Not only equality can be tested. In addition, these Perl operators are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | supported: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | eq | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | ne | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | lt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | gt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | < | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | <= | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | >= | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | == | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | != | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | =~ like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | The way to specify a different operator C<$op> is to put it as key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | into a hash: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | [ $var, { $op, $value } ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | So, the previous rule comparing $var to "foo" can be written as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | - eq: foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | which is essentially running | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | $var eq "foo" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | in Perl. To perform a numerical comparison, use the C<==> operator, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | - ==: 123 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | which runs a test of C<$var == 123> instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | =head2 Regular Expressions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | Regular expression matching is supported as well, so to verify if $var matches | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | the regular expression C</^foo/>, use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | - like: "^foo" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | - $var1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | - =~: "^foo" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | Both are equivalent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | Regular expressions are given without delimiters, e.g. if you want to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | match against /abc/, simply use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | - '$var' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | - like: abc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | To add regex modifiers like C</i> or C</ms>, use the C<(?...)> syntax. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | setting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | - '$var' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | - like: (?i)abc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | will match like C<$var =~ /abc/i>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | =head2 Logical NOT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | A logical NOT is expressed by putting an exclamation mark in front of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | the variable, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | ["!$var1", "foo"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | will return true if $var1 is NOT equal to "foo". The YAML notation is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | - "!$var1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | for this logical expression. Note that YAML requires putting a string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | starting with an exclatmation mark in quotes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | By default, additional rules are chained up with a logical AND operator, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | so to check if a variable is not set to "foo" and not set to "bar", use: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | - '!$var' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | - '!$var' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | - bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | And to verify that the variable matches neither /^foo.*/ nor /^bar.*/, use: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | - '!$var' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | - like: "^foo.*" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | - like: "^bar.*" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | Also note that "^foo.*" requires quotes in YAML. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | =head2 Logical OR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | To specify a rule that is satisfied if I<any> of a series of tests | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | succeeds, use the 'or' keyword in place of a variable: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | [ "or", [ $var, "foo", $var, "bar" ] ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | This data structure indicates that the entire test is supposed to return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | true if either C<$var eq "foo"> or C<$var eq "bar"> holds true. It looks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | like this in YAML: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | - or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | - bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | Pay close attention to the indentation: After the C<- or> follows a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | line with a dash at the same indentation level, followed by a sub-array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | which has its elements indented to the next level. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | =head2 Logical AND | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | By default, YAML::Logic chains up clauses by logical ANDs, i.e. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | - $var1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | - $var2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | - bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | checks if $var1 is equal to "foo" I<and> $var2 is equal to "bar". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | Alternatively, the "and" keyword can be used similar to the "or" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | keyword explained in the previous section: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | - and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | - bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | With the above, you can't have variables named "and" or "or". If you do, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | use a hash key, as explained below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | =head2 Defined-ness | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | The Template Toolkit interpolates undefined variables as empty strings. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | But using TT's virtual methods, you can test if a variable is defined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | in the template context or not. The YAML logic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | - $var1.defined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | - 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | will return true if C<$var1> has been defined. Conversely, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | - $var1.defined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | - "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | will return true if the variable C<$var1> is I<not> defined. Note that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | defined returns 1 on definedness and the empty string ("") if the variable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | is not defined. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | =head2 Logical Set Operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | (not yet implemented) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | - $var1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | - element1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | - element2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | (not yet implemented) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | =for test "yaml" begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | - $var1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | - like: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | - element1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | - element2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | =for test "yaml" end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | =head1 YAML Traps | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | The original YAML implementation has a number of nasty bugs (e.g. RT42015), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | so using YAML::Syck is recommended, which is a both faster and more | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | reliable parser. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | Also, YAML as a configuration format can be tricky at times. For example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | if you type in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | my $data = Load(q{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | # is $var equal to "foo"? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | literally (like in the SYNOPSIS section of this document), keeping the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | indentation intact, YAML will complain that it's not happy about the final | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | blank line, which contains whitespace characters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | Code: YAML_PARSE_ERR_NO_FINAL_NEWLINE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | To avoid this, either use a YAML file, in which not using unnecessary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | indentation will feel natural. When using YAML strings, make sure there's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | no last line containing just whitespace, before feeding it to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | YAML parser: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | my $yaml_string = q{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | # is $var equal to "foo"? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | - foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | $yaml_string =~ s/^\s+\Z//m; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | my $data = Load($yaml_string); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | Also, certain characters have a special meaning in YAML, so you can't write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | # WRONG | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | - !blah! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | because YAML will parse that to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | [$var, undef] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | within the C<rule> hash entry. Why? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | Lines starting with an exclamation mark are I<tags> in YAML. To | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | avoid getting tripped up by this, use quotes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | # CORRECT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | - $var | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | - "!blah!" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | which correctly parses to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | [$var, "!blah!"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | within the C<rule> hash entry instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | =head1 ERROR HANDLING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | If a rule fails, the error() method can be used to obtain a detailed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | textual description on why a comparison or a regex match failed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | if( $logic->evaluate( $data->{rule}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | { var => "foo" }) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | print "True!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | print "Failed, reason is: ", $logic->error(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | This will print something like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | Failed, reason is: Test ["foo" eq "bar"] returned [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | saying the when it compared "foo" to "bar", the result was the empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | string (Perl's idea of 'false'). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | =head1 TROUBLESHOOTING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | =item B<Error Message: Unknown type: HASH(0x857d51c) at YAML/Logic.pm> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | This means that you've fed a hash to YAML::Logic. For example, if your | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | YAML file says | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | rule: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | - "foo" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | - "bar" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | you've probably read the YAML file like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | my $data = LoadFile( $file ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | and now the data looks like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | { rule => ["foo", "bar"] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | which, when you feed it unmodified to YAML::Logic as in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | $logic->evaluate( $data ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | presents the "rule" field to YAML::Logic, which it doesn't understand. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | Pass the content of the rule to YAML::Logic instead: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | $logic->evaluate( $data->{rule} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | and it will work as expected. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | =head1 LEGALESE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | Copyright 2008 by Mike Schilli, all rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | This program is free software, you can redistribute it and/or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 2008, Mike Schilli <cpan@perlmeister.com> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||