From the interpreters point of view any YCP value is an expression and thus can be evaluated. How the evaluation is done in a particular case depends on the data type of the expression.
Because the block data type is somewhat special with respect to evaluation it will be explained first. The other basic data types will follow thereafter.
A YCP block is a sequence of statements enclosed in curly brackets. Upon evaluation (execution), all the statements in the block are evaluated one by one. Because blocks are also a valid data type in YCP, they can have a value (see Data type block). If a block contains the special statement return(...), then the returned value replaces the block upon evaluation.
The following code sample shows a block with some statements.
{ integer n = 1; while (n <= 10) { y2milestone("Number: %1", n); n = n + 1; } y2milestone("Returned number: %1", n); return n; } |
It calculates the numbers 1 through 10 and prints these numbers into the log file. The statement y2milestone(...) used for this is explained in YaST2 Logging along with YCP-logging as such. For now we are interested in the output that is written to the log file. As can be seen below the loop is executed 10 times and the counter has the value 11 after the loop. Finally the last statement return(...) determines the value of the whole block, in this case 11.
...ycp/block_01.ycp:6 Number: 1 ...ycp/block_01.ycp:6 Number: 2 ...ycp/block_01.ycp:6 Number: 3 ...ycp/block_01.ycp:6 Number: 4 ...ycp/block_01.ycp:6 Number: 5 ...ycp/block_01.ycp:6 Number: 6 ...ycp/block_01.ycp:6 Number: 7 ...ycp/block_01.ycp:6 Number: 8 ...ycp/block_01.ycp:6 Number: 9 ...ycp/block_01.ycp:6 Number: 10 ...ycp/block_01.ycp:10 Returned number : 11 |
The basic YCP data types we got to know in Section 3.2, “YCP Data Types” are evaluated in a rather straight forward way as will be shown in the following list.
Evaluation of basic data types
Simple data types
Most of the YCP data types can't be “evaluated” at all, as they simply evaluate to themselves. This holds for the simple types void, boolean, integer, float, string, symbol and path.
list
When evaluating a list, the interpreter evaluates all the list elements thereby forming a new list.
{ list list_var = [ 1 + 1, true || false, "foo" + "bar" ]; y2milestone("list_var: %1", list_var ); } |
yields the log file entry
...ycp/list_eval.ycp:4 list_var: [2, true, "foobar"] |
map
A map is handled similar to a list. The values (but not the keys) are evaluated to form a new map.
{ map map_var = $[ "one" : `one, `two : "one" + "one" ]; y2milestone("map_var: %1", map_var ); } |
yields the log file entry
...ycp/map_eval.ycp:4 map_var: $["one":`one, `two:"oneone"] |
term
Upon evaluation, term parameters are evaluated to form a new term.
{ term term_var = `val ( 1 + 1, true || false, "foo" + "bar" ); y2milestone("term_var: %1", term_var ); } |
yields the log file entry
...ycp/term_eval.ycp:4 term_var: `val(2, true, "foobar") |
All the evaluations we have seen above are closely related to operators that may be used within an expression to act on the variables. The next section will give an overview of the operators that can be used in YCP.