next up previous contents
Next: Silence is sometimes golden Up: Command Files Previous: Tour mode

Passing parameters to scripts

It is possible to have a script use different names for Macaulay objects, each time you call it. If you call a script by the sequence ``<script aa bbb c'', then Macaulay will substitute the strings ``aa'', ``bbb'', and ``c'' anyplace in the text of ``script'' that it finds the strings #1, #2, and #3. In addition, #0 stands for the number of arguments used when the script is called (in this case, 3). More generally, any valid Macaulay expression that produces an integer can be used in place of #1, #2, #3 (#0 can only be the digit 0). The expression will be read up to the next blank character, to figure out which integer you mean, unless it begins with a digit, or a parenthesis ``(``. A leading digit is used by itself, and a parenthesis are read until the matching parenthesis is found. Thus if i has value 3, then in our example, ``a#19 b#(i)b'' will be translated to ``aa9 bcb''. In particular, numbers greater than 9 must always be parenthesized, as in #(19). There is a rather large limit to the number of possible input parameters which Macaulay can handle at any one time; we'd love to hear from you if you bump into it.

Macaulay's parameter passing scheme is best understood if you realize that the core of the program has no idea that all this stuff is happening; all of the effects described here are handled by a single preprocessing routine that hands lines of input to Macaulay as if you typed them. In particular, all Macaulay variables are accessible at all times; there is no internal concept of local variables, since the inner depths of Macaulay have no idea when the voice changes. This can be a real pain if several of your scripts use the same variable name in conflicting ways.

To avoid this problem, we created a way for scripts to uniquely name their temporary variables: the character @ is translated to @nn@, where ``nn'' is the current voice level. Thus if a script running as voice 1 uses a temporary name i@, and it calls a script which also uses a variable i@, then the two names will read as i@01@ and i@02@. These variables are not automatically erased, but if later a new script running as voice 2 uses i@, it will write over the old i@02@ (which is OK with us if it's OK with you).

There are two exceptions to the automatic expansion of @: Macaulay recognizes something that looks like it is already expanded, and doesn't reexpand it (so i@01@ doesn't become i@01@01@01@), and when the input voice is from the keyboard, Macaulay assumes that you mean the closest voice associated with a script (so i@ becomes i@01@ in voice 0, and i@02@ in voice 3, for example.) This is handy when debugging.

If a script needs to use a name which isn't intended to be temporary, it should be passed as a parameter to the script, to avoid name conflicts. If a script needs to use a name which is intended to be temporary, it should kill the variable after it is done with it, in case space usage becomes a problem.

If you want to create scripts interactively which use parameters, then pass parameters to the script as you create it: Enter ``>>script dd eee f'' to use ``dd'', ``eee'', and ``f'' as example names. While recording this script, you must use #1, #2, and #3 to refer to these parameters if you want your commands to change whene the parameters change. If you use ``dd'', ``eee'', and ``f'' while writing your script, it will be hard-wired to use these names, even if it is later called via the sequence ``<script aa bbb c''.


next up previous contents
Next: Silence is sometimes golden Up: Command Files Previous: Tour mode

Sorin Popescu
Fri Feb 14 17:37:19 EST 1997