Packages
Packages let you declare global variables and functions whose name space is localized to one or more command files. You declare a package using the
package command at the beginning of a file. Here is an example:
package myapp
This package defines a new symbol table in which all global variables and functions in the remainder of the file are placed. You can refer to variables and functions in other packages by adding them to the package name with two colons (::). Here are some examples:
main::selection, history::set_cmd_line(), myapp::myvar
Predefined variables such as doctype, status, selection, etc., are defined in the package main. If you want to reference these predefined variables and functions inside a new package, the new package must be preceded by the main package, such as:
main::status
In packages other than main, it's not necessary for a dollar sign ($) to precede a variable term inside an expression.
All function names defined in a package are local to the package. It is possible, however, to "export" the function name to the main package (or another package) by using a package prefix on the function definition. Here is an example:
function main::fac(n) { ...}
|
It's best not to define functions (and aliases) that will be exported to the main package because this could conflict with Arbortext Editor internal functions or with new built-in functions provided in later Arbortext Editor releases.
|
The example shown above puts the function name fac into the main package, but any variables in the function body are still local to the current package.
The package declaration may also be used within a function definition to switch the current package. The scope of the package declaration is the same as the local command. That is, the previous package is restored when the end of the block is reached, which is indicated by a right curly bracket (}), or another package command is executed. Here is an example:
package myapp
status=0
function f(x)
{
# refers to myapp::status
status = 1;
{
package main
# refers to main::status
status = 0;
}
# refers to myapp::status
status = 2;
}
If you're in a package other than main, and that package calls out functions (including built-in functions) defined in main, it's not necessary to precede those functions with the main package prefix. When a function call without a package prefix is compiled, the parser looks up the name of the function in the active function list. If the function name isn't found, the parser tries to determine if the function is a user-defined or built-in function in the main package.
Unlike package main, a reference to an undefined variable does not refer to an environment variable of the same name. For example, in the main package, $PATH normally refers to the path environment variable. In any other package, $PATH refers to a package local variable.
The name space for aliases is not affected by packages. Aliases are always compiled as if they had been defined in the main package. Like the local command, the package command is not valid in an alias. To refer to variables (and functions) that aren't in the main package, you must use the package name prefix within an alias.
Built-in Arbortext Editor commands may also be prefixed with the main:: qualifier. This allows you to define an alias that unambiguously refers to a primitive command. Here is an example:
alias myquit {
# do something
main::quit; # really quit
}
Without the prefix, the reference to quit might actually invoke some alias of quit.
The
package function can be used to change the current package to a package previously specified by the
package command.