Conditionals Can Nest
V8 sendmail allows conditionals to nest. As an example, consider the following expression:
$?x $?y both $| xonly $. $| $?y yonly $| none $. $.
This is just like the example in the previous section:
$?x text1 $| text2 $.
except that text1
and text2
are
both conditionals:
text1 = $?y both $| xonly $. text2 = $?y yonly $| none $.
The grouping when conditionals nest is from the outside in. In the following example, parentheses have been inserted to show the groupings (they are not a part of either expression):
(
$?x(
text1)
$|(
text2)
$.)
(
$?x(
$?y both $| xonly $.)
$|(
$?y yonly $| none $.)
$.)
Interpretation is from left to right. The logic of the
second line is therefore this: if both $x
and $y
have values, the
result is both
.
If $x
has a value
but $y
lacks one,
the result is xonly
. If $x
lacks a value but $y
has one, the result
is yonly
. And if
both lack values, the result is none
.
The sendmail program does not
enforce or check for balance in nested conditionals.
Each $?
should
have a corresponding $
. to balance it. If they do not
balance, sendmail will not
detect the problem. Instead, it might interpret the
expression in a way that you did not intend.
The depth to which conditionals can be nested is limited only by our ability to easily comprehend the result. More than two deep is not recommended, and more than three deep is vigorously discouraged.
Get sendmail, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.