Next: Variables, Previous: Sendmail Macros, Up: MFL [Contents][Index]
A constant is a symbolic name for an MFL value.
Constants are defined using const
statement:
[qualifier] const name expr
where name is an identifier, and expr is any valid
MFL expression evaluating immediately to a constant literal
or numeric value. Optional qualifier defines the scope of
visibility for that constant (see scope of visibility): either
public
or static
.
Once defined, any appearance of name in the program text is replaced by its value. For example:
const x 10/5 const text "X is "
defines the numeric constant ‘x’ with the value ‘5’, and the literal constant ‘text’ with the value ‘X is ’.
A special construct is provided to define a series of numeric constants (an enumeration):
[qualifier] const do name0 [expr0] name1 [expr1] ... nameN [exprN] done
Each exprN, if present, must evaluate to a constant numeric expression. The resulting value will be assigned to constant nameN. If exprN is not supplied, the constant will be defined to the value of the previous constant plus one. If expr0 is not supplied, 0 is assumed.
For example, consider the following statement
const do A B C 10 D done
This defines ‘A’ to 0, ‘B’ to 1, ‘C’ to 10 and ‘D’ to 11.
As a matter of fact, exprN may also evaluate to a constant string expression, provided that all expressions in the enumeration ‘const’ statement are provided. That is, the following is correct:
const do A "one" B "two" C "three" D "four" done
whereas the following is not:
const do A "one" B C "three" D "four" done
Trying to compile the latter example will produce:
mailfromd: filename:5.3: initializer element is not numeric
which means that mailfromd
was trying to create constant
‘B’ with the value of ‘A’ incremented by one, but was unable
to do so, because the value in question was not numeric.
Constants can be used in normal MFL expressions as well as in literals. To expand a constant within a literal string, prepend a percent sign to its name, e.g.:
echo "New %text %x" ⇒ "New X is 2"
This way of expanding constants creates an ambiguity if there happen to be a variable of the same name as the constant. See variable--constant clashes, for more information of this case and ways to handle it.
• Built-in constants |
Several constants are built into the MFL compiler. To discern them from user-defined ones, their names start and end with two underscores (‘__’).
The following constants are defined in mailfromd
version
9.0:
Expands to the name of the current source file.
Expands to the name of the current lexical context, i.e. the function or handler name.
This built-in constant is defined for alpha versions only. Its value is the Git tag of the recent commit corresponding to that version of the package. If the release contains some uncommitted changes, the value of the ‘__git__’ constant ends with the suffix ‘-dirty’.
Expands to the current line number in the input source file.
Expands to the major version number.
The following example uses __major__
constant to determine
if some version-dependent feature can be used:
if __major__ > 2
# Use some version-specific feature
fi
Expands to the minor version number.
Expands to the name of the current module (see Modules).
Expands to the package name (‘mailfromd’)
For alpha versions and maintenance releases expands to the version patch level. For stable versions, expands to ‘0’.
Expands to the default external preprocessor command line, if the preprocessor is used, or to an empty string if it is not, e.g.:
__defpreproc__ ⇒ "/usr/bin/m4 -s"
See Preprocessor, for information on preprocessor and its features.
Expands to the current external preprocessor command line, if the
preprocessor is used, or to an empty string if it is not. Notice,
that it equals __defpreproc__
, unless the preprocessor was
redefined using --preprocessor command line option
(see –preprocessor).
Expands to the textual representation of the program version (e.g. ‘3.0.90’)
Expands to the default state directory (see statedir).
Expands to the current value of the program state directory
(see statedir). Notice, that it is the same as
__defstatedir__
unless the state directory was redefined at run
time.
Built-in constants can be used as variables, this allows to expand them within strings or here-documents. The following example illustrates the common practice used for debugging configuration scripts:
func foo(number x) do echo "%__file__:%__line__: foo called with arg %x" … done
If the function foo
were called in line 28 of the
script file /etc/mailfromd.mfl
, like this:
foo(10)
, you will see the following string in your logs:
/etc/mailfromd.mfl:28: foo called with arg 10