Operators and Expressions
Pikt provides the usual operators, and a few not so usual. Most of them are borrowed from Perl, but several are unique to Pikt.
Here are the logical operators:
LOGICAL DESCRIPTION && logical AND || logical OR ! logical NOT ( ) logical grouping(Note that for config file #if-#endif preprocessing, you use the & and | set operators, not && or ||.)
The && and || operators use short-circuiting evaluation.
Except in some arithmetical or complex logical expressions, parentheses are often unnecessary. You're sometimes better off avoiding them.
For example, you may do this (in a macro definition):
bypass_weekend if ( =friday && =evening ) || ( =saturday ) || ( =sunday && ! =evening ) quit fibut you may also, and with equivalent effect, do this:
bypass_weekend if =friday && =evening || =saturday || =sunday && ! =evening quit fi
Some comments about formatting:
As we indicated in Format, Pikt is generally indifferent to script and config file layout, and you may lay out your config files and Pikt scripts in any style that pleases you.
You may do this, attempting to clarify the if logic with carefully crafted layout
rule // non-root uid 0's if $uid eq "0" && ! #nis && ( $uname !~ "^(root|sundiag|sysdiag|smtp)$" || ( $uname =~ "^(sundiag|sysdiag)$" && $password ne "*" ) ) output mail "User $uname has UID OF 0!" endifbut you may also do this
rule // non-root uid 0's if $uid eq "0" && ! #nis && ($uname !~ "^(root|sundiag|sysdiag|smtp)$" || ($uname =~ "^(sundiag|sysdiag)$" && $password ne "*")) output mail "User $uname has UID OF 0!" endifor (crazily) even this
rule // non-root uid 0's if $uid eq "0" && ! #nis && ( $uname !~ "^(root|sundiag|sysdiag|smtp)$" || ( $uname =~ "^(sundiag|sysdiag)$" && $password ne "*" ) ) output mail "User $uname has UID OF 0!" endifIn each case, the formatting is irrelevant, and all three examples will have the exact same effect.
(Note that, any special formatting you apply in the config files don't apply in the installed script on the PIKT slave systems. In the installed versions, standard formatting applies. Here is how the above example would appear after PIKT installs it on the slave system:
rule if $uid eq "0" && ! #nis && ($uname !~ "^(root|sundiag|sysdiag|smtp)$" || ($uname =~ "^(sundiag|sysdiag)$" && $password ne "*")) output mail "User $uname has UID OF 0!" endifthat is, exactly as in the second (sensible) example above. (Actually, the 'if' line above would appear all on a single line. We have broken it up into two separate lines in this example for purposes of displaying it properly here--that is, not running off the right side of the web page.)
Here are the Pikt arithmetic operators:
ARITHMETIC DESCRIPTION + add - subtract * multiply / divide % modulus ** exponentiate & bit AND | bit OR ^ bit EXOR ~ bit COMP << bit left shift >> bit right shift == equals > greater than < less than >= greater than or equal to <= less than or equal to != not equal to <> not equal to !> not greater than !< not less than !>= not greater than or equal to !<= not less than or equal toNote the absence of the auto-increment and auto-decrement operators. Instead, you would use += and -= (see below).
The last four operators above are a bit unusual, and note that != and <> are interchangeable.
Here are the Pikt string operators:
STRING DESCRIPTION . concatenate eq is identical to ne is not identical to lt precedes in dictionary order gt follows in dictionary order le precedes in dictionary order, or is identical to ge follows in dictionary order, or is identical toAside from concatenation, there are many more things one can do with strings using the built-in string functions.
Here are the Pikt assignment operators:
ASSIGNMENT DESCRIPTION = assign to variable, used with all data types (string, number, filehandle) += add and assign -= subtract and assign *= multiply and assign /= divide and assign %= modulus and assign **= exponentiate and assign &= bit AND and assign |= bit OR and assign ^= bit EXOR and assign <<= bit left shift and assign >>= bit right shift and assign .= concatenate and assignFinally, here are the Pikt file test operators:
FILE DESCRIPTION -e exists -z is zero length -f is a regular file -d is a directory -c is a character special file -b is a block special file -p is a pipe -l is a symlink -S is a socketFor example, you could test for the existence of a file as in
rule if ! -e $fil =outputmail "$fil not found!" next endif(proc tests, similar in format to file tests, might be developed for some future PIKT version.)
These operators follow the usual associativity and precedence rules.
So, too, with how objects and operators combine to form expressions. The usual rules apply (no real surprises here).
Refer to the Samples section for many more examples.
prev page | 1st page | next page |