Declarations bind names to values. Variables are declared inside the global section of the rule set, the rule prelude, functions, and user-defined actions. A semicolon separates declarations. The following example shows a prelude that declares two variables, x and y:
x = 5;
y = event:url.extract(#http://(\[^/?]+)#)
KRL is statically scoped. Each place where an identifier is bound in a program corresponds to a region of the program text within which the binding is visible. The region is determined by the particular programmatic context where the binding is made:
- Each rule set is a binding scope and the bindings constructed within a rule set evaluation are only visible within that rule set execution. Any declarations made in the global section of a rule set will be visible for the execution of any rules in that rule set.
- Each rule is also a binding scope. Values bound to names in the setting clause of an eventex and values bound in the prelude are visible throughout the execution of that particular rule.
KRL variable declarations are not variable assignments. Neither can KRL expressions cause persistent side effects. For example the second declaration in the following prelude does not change the value to which x is bound. Rather, it results in two bindings of x, one of which is hidden (shadowed) by the other:
x = 5;
x = 6;
Developers should avoid treating declarations as assignments because they will invariably be surprised by the resulting semantics in subtle ways.