Interfacing with Other Languages
It is possible for scripts written in other languages to invoke pikt scripts, and vice versa. For example, in a Perl script you might have:
system("/pikt/bin/pikt +A Critical");or
system("/pikt/lib/programs/chkdevs.pkt")Pikt scripts interface with other languages in any of the following six ways:
- in an input proc statement
- in a filter statement
- in an exec statement
- within a $command() call
- within a #system() call
- within a #popen() call
When invoking, say, an Awk one-liner within a Pikt script, you must be mindful of how Pikt handles quotes and variable identifiers. Consider this sample script:
KillIdleUserSessionCritical init status active level critical task "Terminate idle user sessions" input proc "=w | =nawk '/[1-9]day/ {gsub("\\/","\\\\/"); print $1 " " $2}'" dat $user 1 dat $tty 2 rule exec wait "=kill -9 `=ps -ef | =nawk '/$user.+$tty/ {print \$2}'`"In the sample above, note the single and double quotes embedded within the input proc string. This is perfectly acceptable, but in weird circumstances you can tie yourself up in the quoting tangle common to all script languages. Sometime you have to resort to using the $dquote(), $squote(), and/or $char() functions, possibly in conjunction with the "." concatenation operator, to undo the tangle.
In the sample, you want the end-product nawk function to be
gsub("\/","\\/")but to achieve this you have to backslash escape each "\" going in. (You need to transform, for example, "pts/3" into "pts\/3" because you will use the second string in the exec statement's nawk command following.)
Note that in the first nawk command, the "$1" and "$2" are not backslash-escaped, while in the second nawk command the "\$2" is. The reason for this is that, at the point of any input proc statement, no variables have been defined yet, so no suppression of variable resolution is necessary. With a rule (or begin or end), however, you must escape, e.g., $2 for otherwise Pikt will attempt to resolve it as a pattern-match variable. If this inconsistency troubles you, you could easily (and needlessly) escape (using a single "\") Awk variables in the input proc statement; the effect would be the same.
Pikt was designed to interface readily with other languages. If you prefer to use the Pikt script language minimally, and just wrap simple Pikt scripts around more elaborate scripts written in Perl, Python or whatever, that's entirely your choice.
prev page | 1st page | next page |