1Config::Model::ValueComUpsuetrerC(o3n)tributed Perl DocuCmoennftiagt:i:oMnodel::ValueComputer(3)
2
3
4
6 Config::Model::ValueComputer - Provides configuration value computation
7
9 version 1.205
10
12 my $model = Config::Model->new() ;
13
14 $model ->create_config_class
15 (
16 name => "Master",
17 element
18 => [
19 [qw/av bv/] => {type => 'leaf',
20 value_type => 'integer',
21 },
22 compute_int
23 => { type => 'leaf',
24 value_type => 'integer',
25 compute => { formula => '$a + $b',
26 variables => { a => '- av', b => '- bv'}
27 },
28 min => -4,
29 max => 4,
30 },
31 [qw/sav sbv/] => {type => 'leaf',
32 value_type => 'string',
33 },
34 compute_string
35 => { type => 'leaf',
36 value_type => 'string',
37 compute => { formula => 'meet $a and $b',
38 variables => { '- sav', b => '- sbv' }
39 },
40 },
41 compute_with_replace
42 => { type => 'leaf',
43 value_type => 'string',
44 compute => {
45 formula => '$replace{$who} is the $replace{$what} of $replace{$country}',
46 variables => {
47 who => '! who' ,
48 what => '! what' ,
49 country => '- country',
50 },
51 replace => { chief => 'president',
52 America => 'USA'
53 },
54 },
55 },
56
57 url => { type => 'leaf', value_type => 'uniline'},
58 extract_host_from_url
59 => { type => 'leaf',
60 value_type => 'uniline',
61 compute => { formula => '$old =~ m!http://([\w\.]+)!; $1 ;' ,
62 variables => { old => '- url' } ,
63 use_eval => 1 ,
64 },
65 },
66 ]
67 ) ;
68
70 This class provides a way to compute a configuration value. This
71 computation uses a formula and some other configuration values from the
72 configuration tree.
73
74 The computed value can be overridden, in other words, the computed
75 value can be used as a defult value.
76
78 A computed value must be declared in a 'leaf' element. The leaf element
79 must have a "compute" argument pointing to a hash ref.
80
81 This array ref contains:
82
83 · A string formula that use variables and replace function.
84
85 · A set of variable and their relative location in the tree (using
86 the notation explained in grab() method
87
88 · An optional set of replace rules.
89
90 · An optional parameter to force a Perl eval of a string.
91
92 Compute formula
93 The first element of the "compute" array ref must be a string that
94 contains the computation algorithm (i.e. a formula for arithmetic
95 computation for integer values or a string template for string values).
96
97 This string or formula should contain variables (like $foo or $bar).
98 Note that these variables are not interpolated by perl.
99
100 For instance:
101
102 'My cat has $nb legs'
103 '$m * $c**2'
104
105 This string or formula may also contain:
106
107 · The index value of the current object : &index or "&index()".
108
109 · The index value of another object: "&index($other)"
110
111 · The element name of the current object: &element or "&element()".
112
113 · The element name of another object: "&element($other)"
114
115 For instance, you could have this template string:
116
117 'my element is &element, my index is &index' .
118 'upper element is &element($up), upper index is &index($up)',
119
120 If you need to perform more complex operations than substition, like
121 extraction with regular expressions, you can force an eval done by Perl
122 with "use_eval => 1". In this case, the result of the eval will be used
123 as the computed value.
124
125 For instance:
126
127 # extract host from url
128 compute => { formula => '$old =~ m!http://[\w\.]+(?::\d+)?(/.*)!; $1 ;',
129 variables => { old => '- url' } ,
130 use_eval => 1 ,
131 },
132
133 # capitalize
134 compute => { formula => 'uc($old)',
135 variables => { old => '- small_caps' } ,
136 use_eval => 1
137 }
138
139 Compute variables
140 The following arguments will be a set of "key => value" to define the
141 variables used in the formula. The key is a variable name used in the
142 computation string. The value is a string that will be used to get the
143 correct Value object.
144
145 In this numeric example, "result" default value is "av + bv":
146
147 element => [
148 av => {
149 type => 'leaf',
150 value_type => 'integer'
151 },
152 bv => {
153 type => 'leaf',
154 value_type => 'integer'
155 },
156 result => {
157 type => 'leaf',
158 value_type => 'integer',
159 compute => { formula => '$a + $b' ,
160 variables => { a => '- av', b => '- bv' },
161 }
162 }
163
164 In this string example, the default value of the "Comp" element is
165 actually a string made of ""macro is "" and the value of the ""macro""
166 element of the object located 2 nodes above:
167
168 comp => {
169 type => 'leaf',
170 value_type => 'string',
171 compute => { formula => '"macro is $m"' ,
172 variables => { m => '- - macro' }
173 }
174 }
175
176 Compute replace
177 Sometime, using the value of a tree leaf is not enough and you need to
178 substitute a replacement for any value you can get. This replacement
179 can be done using a hash like notation within the formula using the
180 %replace hash.
181
182 For instance, if you want to display a summary of a config, you can do
183 :
184
185 compute_with_replace
186 => {
187 formula => '$replace{$who} is the $replace{$what} of $replace{$country}',
188 variables => {
189 who => '! who' ,
190 what => '! what' ,
191 country => '- country',
192 },
193 replace => { chief => 'president',
194 America => 'USA'
195 },
196
197 Complex formula
198 &index, &element, and replace can be combined. But the argument of
199 &element or &index can only be a value object specification (I.e.
200 something like '"- - foo"'), it cannot be a value replacement of
201 another &element or &index.
202
203 I.e. "&element($foo)" is ok, but "&element(&index($foo))" is not
204 allowed.
205
206 computed variable
207 Compute variables can themselves be computed :
208
209 compute => {
210 formula => 'get_element is $replace{$s}, indirect value is \'$v\'',
211 variables => { 's' => '! $where',
212 where => '! where_is_element',
213 v => '! $replace{$s}',
214 }
215 replace => { m_value_element => 'm_value',
216 compute_element => 'compute'
217 }
218 }
219
220 Be sure not to specify a loop when doing recursive computation.
221
222 compute override
223 In some case, a computed value must be interpreted as a default value
224 and the user must be able to override this computed default value. In
225 this case, you must use "allow_override => 1" with the compute
226 parameter:
227
228 computed_value_with_override => {
229 type => 'leaf',
230 value_type => 'string',
231 compute => { formula => '"macro is $m"' ,
232 variables => { m => '- - macro' } ,
233 allow_override => 1,
234 }
235 }
236
238 Dominique Dumont, (ddumont at cpan dot org)
239
241 Config::Model, Config::Model::Instance, Config::Model::Value
242
243
244
245perl v5.12.1 2010-08-18 Config::Model::ValueComputer(3)