line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
########################################### |
2
|
|
|
|
|
|
|
package YAML::Logic; |
3
|
|
|
|
|
|
|
########################################### |
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
4889
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
66
|
|
6
|
3
|
|
|
3
|
|
20
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
88
|
|
7
|
3
|
|
|
3
|
|
2035
|
use Log::Log4perl qw(:easy); |
|
3
|
|
|
|
|
100060
|
|
|
3
|
|
|
|
|
12
|
|
8
|
3
|
|
|
3
|
|
2384
|
use Template; |
|
3
|
|
|
|
|
45838
|
|
|
3
|
|
|
|
|
86
|
|
9
|
3
|
|
|
3
|
|
1567
|
use Data::Dumper; |
|
3
|
|
|
|
|
12775
|
|
|
3
|
|
|
|
|
143
|
|
10
|
3
|
|
|
3
|
|
1337
|
use Safe; |
|
3
|
|
|
|
|
70606
|
|
|
3
|
|
|
|
|
2707
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = "0.06"; |
13
|
|
|
|
|
|
|
our %OPS = map { $_ => 1 } |
14
|
|
|
|
|
|
|
qw(eq ne lt gt < > <= >= == =~ like); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
########################################### |
17
|
|
|
|
|
|
|
sub new { |
18
|
|
|
|
|
|
|
########################################### |
19
|
67
|
|
|
67
|
0
|
12485
|
my($class, %options) = @_; |
20
|
|
|
|
|
|
|
|
21
|
67
|
|
|
|
|
202
|
my $self = { |
22
|
|
|
|
|
|
|
safe => Safe->new(), |
23
|
|
|
|
|
|
|
template => Template->new(), |
24
|
|
|
|
|
|
|
error => "", |
25
|
|
|
|
|
|
|
%options, |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
|
28
|
67
|
|
|
|
|
103336
|
$self->{safe}->permit(); |
29
|
|
|
|
|
|
|
|
30
|
67
|
|
|
|
|
476
|
bless $self, $class; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
########################################### |
34
|
|
|
|
|
|
|
sub interpolate { |
35
|
|
|
|
|
|
|
########################################### |
36
|
251
|
|
|
251
|
0
|
3608
|
my($self, $input, $vars) = @_; |
37
|
|
|
|
|
|
|
|
38
|
251
|
100
|
|
|
|
399
|
if(ref($input) eq "HASH") { |
39
|
|
|
|
|
|
|
# When working on the original, we got weird memory errors |
40
|
|
|
|
|
|
|
# in perl5.8, so just copy the hash. |
41
|
35
|
|
|
|
|
92
|
my %dupe = %$input; |
42
|
35
|
|
|
|
|
45
|
my @keyvals = (); |
43
|
35
|
|
|
|
|
54
|
for my $entry (each %dupe) { |
44
|
70
|
|
|
|
|
116
|
push @keyvals, $self->interpolate( $entry, $vars ); |
45
|
|
|
|
|
|
|
} |
46
|
35
|
|
|
|
|
121
|
return { @keyvals }; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
216
|
|
|
|
|
159
|
my $out; |
50
|
216
|
|
|
|
|
231
|
$input =~ s/(?:\$\{([\w.]+)})/[%- $1 %]/gx; |
51
|
216
|
|
|
|
|
413
|
$input =~ s/(?:\$([\w.]+)) /[%- $1 %]/gx; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$self->{template}->process( \$input, $vars, \$out ) or |
54
|
216
|
50
|
|
|
|
592
|
LOGDIE $self->{template}->error(); |
55
|
|
|
|
|
|
|
|
56
|
216
|
|
|
|
|
166150
|
return $out; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
########################################### |
60
|
|
|
|
|
|
|
sub evaluate { |
61
|
|
|
|
|
|
|
########################################### |
62
|
91
|
|
|
91
|
0
|
2608
|
my($self, $data, $vars, $not_glob, $boolean_or) = @_; |
63
|
|
|
|
|
|
|
|
64
|
91
|
50
|
|
|
|
165
|
$not_glob = 0 unless defined $not_glob; |
65
|
91
|
100
|
|
|
|
111
|
$boolean_or = 0 unless defined $boolean_or; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
0
|
|
0
|
DEBUG sub { "evaluate: " . |
68
|
|
|
|
|
|
|
Dumper( $data ) . "\n" . |
69
|
|
|
|
|
|
|
Dumper( $vars ) . "\n" . |
70
|
|
|
|
|
|
|
"not_glob=$not_glob " . |
71
|
|
|
|
|
|
|
"boolean_or=$boolean_or " . |
72
|
|
|
|
|
|
|
"" |
73
|
91
|
|
|
|
|
380
|
}; |
74
|
|
|
|
|
|
|
|
75
|
91
|
50
|
|
|
|
659
|
if( ref($data) eq "ARRAY" ) { |
76
|
91
|
|
|
|
|
146
|
my @data = @$data; # make a copy, so splice() doesn't destroy |
77
|
|
|
|
|
|
|
# the original. |
78
|
91
|
|
|
|
|
218
|
while( my($field, $value) = splice @data, 0, 2 ) { |
79
|
110
|
|
|
|
|
83
|
my $res; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $not; |
82
|
|
|
|
|
|
|
|
83
|
110
|
50
|
|
|
|
152
|
if(! defined $field) { |
84
|
0
|
|
|
|
|
0
|
LOGDIE "Rule variable not defined (value=$value)", |
85
|
|
|
|
|
|
|
"(maybe YAML rule: !\$var without quotes?"; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
110
|
100
|
|
|
|
204
|
if($field =~ s/^!//) { |
89
|
10
|
|
|
|
|
11
|
$not = !$not_glob; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
110
|
100
|
|
|
|
191
|
if($field eq "or") { |
|
|
100
|
|
|
|
|
|
93
|
18
|
|
|
|
|
35
|
$res = $self->evaluate($value, $vars, $not, 1); |
94
|
|
|
|
|
|
|
} elsif( $field eq "and") { |
95
|
8
|
|
|
|
|
16
|
$res = $self->evaluate($value, $vars, $not); |
96
|
|
|
|
|
|
|
} else { |
97
|
84
|
|
|
|
|
137
|
$field = $self->interpolate($field, $vars); |
98
|
84
|
|
|
|
|
159
|
$value = $self->interpolate($value, $vars); |
99
|
|
|
|
|
|
|
|
100
|
84
|
100
|
|
|
|
181
|
if(ref($value) eq "") { |
|
|
50
|
|
|
|
|
|
101
|
49
|
|
|
|
|
102
|
$res = $self->evaluate_single( $field, $value, "eq", $not ); |
102
|
|
|
|
|
|
|
} elsif(ref($value) eq "HASH") { |
103
|
35
|
|
|
|
|
71
|
my($op) = keys %$value; |
104
|
35
|
|
|
|
|
52
|
($value) = values %$value; |
105
|
35
|
|
|
|
|
84
|
$res = $self->evaluate_single( $field, $value, $op, $not ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
109
|
100
|
100
|
|
|
572
|
if($boolean_or and $res) { |
|
|
100
|
100
|
|
|
|
|
110
|
|
|
|
|
|
|
# It's a boolean OR, so all it takes is one true result |
111
|
1
|
|
|
|
|
3
|
my $rc = 1; |
112
|
1
|
|
|
|
|
5
|
DEBUG "evaluate: rc=$rc"; |
113
|
1
|
|
|
|
|
10
|
return $rc; |
114
|
|
|
|
|
|
|
} elsif(!$boolean_or and !$res) { |
115
|
|
|
|
|
|
|
# It's a boolean AND, so all it takes is one false result |
116
|
64
|
|
|
|
|
60
|
my $rc = 0; |
117
|
64
|
|
|
|
|
165
|
DEBUG "evaluate: rc=$rc"; |
118
|
64
|
|
|
|
|
406
|
return $rc; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} else { |
122
|
0
|
|
|
|
|
0
|
LOGDIE "Unknown type: $data"; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Return 1 if all ANDed conditions succeeded, and 0 if all |
126
|
|
|
|
|
|
|
# ORed conditions failed. |
127
|
25
|
|
|
|
|
24
|
my $rc = 1; |
128
|
25
|
100
|
|
|
|
34
|
$rc = 0 if $boolean_or; |
129
|
25
|
|
|
|
|
72
|
DEBUG "evaluate: rc=$rc"; |
130
|
25
|
|
|
|
|
146
|
return $rc; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
########################################### |
134
|
|
|
|
|
|
|
sub evaluate_single { |
135
|
|
|
|
|
|
|
########################################### |
136
|
84
|
|
|
84
|
0
|
108
|
my($self, $field, $value, $op, $not) = @_; |
137
|
|
|
|
|
|
|
|
138
|
84
|
|
|
|
|
95
|
$op = lc $op ; |
139
|
84
|
100
|
|
|
|
147
|
$op = '=~' if $op eq "like"; |
140
|
|
|
|
|
|
|
|
141
|
84
|
|
|
|
|
120
|
$self->error(""); |
142
|
|
|
|
|
|
|
|
143
|
84
|
50
|
|
|
|
146
|
if(! exists $OPS{ $op }) { |
144
|
0
|
|
|
|
|
0
|
LOGDIE "Unknown op: $op"; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
84
|
|
|
|
|
100
|
$field = '"' . esc($field) . '"'; |
148
|
84
|
|
|
|
|
80
|
my $cmd; |
149
|
|
|
|
|
|
|
|
150
|
84
|
100
|
|
|
|
97
|
if($op eq "=~") { |
151
|
21
|
100
|
|
|
|
47
|
if($value =~ /\?\{/) { |
152
|
1
|
|
|
|
|
4
|
LOGDIE "Trapped ?{ in regex."; |
153
|
|
|
|
|
|
|
} |
154
|
20
|
|
|
|
|
73
|
$value =~ s#(\\\\|/)#\\$1#g; |
155
|
|
|
|
|
|
|
# If we ever get something like \\/, slap another backslash |
156
|
|
|
|
|
|
|
# onto the "/" to mask it. |
157
|
20
|
100
|
|
|
|
31
|
$value =~ s#(\\+)/# (length($1) % 2) ? "$1/" : "$1\\/"#ge; |
|
12
|
|
|
|
|
38
|
|
158
|
20
|
|
|
|
|
161
|
$value = qr($value); |
159
|
20
|
|
|
|
|
40
|
$cmd = "$field =~ /$value/"; |
160
|
|
|
|
|
|
|
} else { |
161
|
63
|
|
|
|
|
95
|
$value = '"' . esc($value) . '"'; |
162
|
63
|
|
|
|
|
79
|
$cmd = "$field $op $value"; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
83
|
|
|
|
|
208
|
INFO "Test: $cmd"; |
166
|
83
|
|
|
|
|
581
|
my $res = $self->{safe}->reval($cmd); |
167
|
|
|
|
|
|
|
|
168
|
83
|
50
|
|
|
|
26995
|
if($@) { |
169
|
0
|
|
|
|
|
0
|
LOGDIE "Evaling [$cmd] failed: $@"; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
83
|
100
|
66
|
|
|
377
|
if(!$res and !$not or |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
173
|
|
|
|
|
|
|
$res and $not) { |
174
|
73
|
50
|
|
|
|
130
|
$res = "" if !defined $res; |
175
|
73
|
|
|
|
|
186
|
$self->error("Test [$cmd] returned [$res]"); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
83
|
100
|
|
|
|
205
|
return ($not ? (!$res) : $res); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
########################################### |
182
|
|
|
|
|
|
|
sub error { |
183
|
|
|
|
|
|
|
########################################### |
184
|
204
|
|
|
204
|
0
|
28630
|
my($self, $error) = @_; |
185
|
|
|
|
|
|
|
|
186
|
204
|
100
|
|
|
|
317
|
if(defined $error) { |
187
|
157
|
|
|
|
|
171
|
$self->{error} = $error; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
204
|
|
|
|
|
629
|
return $self->{error}; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
############################################### |
194
|
|
|
|
|
|
|
sub esc { |
195
|
|
|
|
|
|
|
############################################### |
196
|
147
|
|
|
147
|
0
|
144
|
my($str, $metas) = @_; |
197
|
|
|
|
|
|
|
|
198
|
147
|
|
|
|
|
203
|
$str =~ s/([\\"])/\\$1/g; |
199
|
|
|
|
|
|
|
|
200
|
147
|
50
|
|
|
|
198
|
if(defined $metas) { |
201
|
0
|
|
|
|
|
0
|
$metas =~ s/\]/\\]/g; |
202
|
0
|
|
|
|
|
0
|
$str =~ s/([$metas])/\\$1/g; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
147
|
|
|
|
|
200
|
return $str; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
1; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
__END__ |