The p context provides functions to support A+ package storage.A package is a single structure containing A+ functions, variables, and dependencies. When a package is stored in a file, it is called a packfile. A packstring is a package in the form of a character array in an A+ workspace. A package can be used for storing a miscellaneous collection of A+ objects in a file, loading an application - it is usually faster than a script -, sending A+ objects through an adap connection, and performing lexical analysis (by a very knowledgeable person).
Packages are manipulated by external functions stored in the p context, which does not need to be loaded. The functions come in pairs: those whose unqualified names begin with s work on packstrings and those whose names begin with f work on packfiles. Package storage is optimized for fast retrieval, at the expense of space where necessary.
A package consists of a number of items, each of which is an object of one of these kinds:
Nothing else can be included in a package. In particular, an item cannot be an s attribute on a variable, the state of a variable's display, an attribute set with _set, a set or preset callback, an external function (i.e., a function loaded using _dyld), or an external state, such as an adap connection.
- an A+ data object (possibly an enclosed primitive function or operator);
- a dependency - whose definition is included but not value, so that the dependency will be evaluated afresh when needed after retrieval;
- a defined function or operator;
- a definition of a mapped file - viz., the name of the file and an integer indicating the type of mapping: the data itself is not included in the package.
The only operations performed during retrieval from a package are:
If some other operation is required in order to restore some information, then you must provide a mechanism supplemental to package retrieval. For example, you could include in a package an initialization routine that sets attributes on variables.
- specification of global variables;
- definition of dependencies;
- definition of functions and operators;
- Mapping (beaming) in of files, using à - note that a file's beamed value may be different from its value when it was put into the package and that there will be a value error if the file was removed in the interim.
Some of the restrictions, such as the one on dynamically loaded external functions, are due to an inability to retrieve the information needed to adequately store the object; they may possibly be lifted in the course of further development of the interpreter. Some objects, such as adap connections, are so complex that full retrieval of them may never be possible.
Other restrictions reflect a need for experience with packages and further thought. Callbacks, for instance, are not included because it is not clear whether during retrieval of a package variables should be specified and then callbacks set (so that no callbacks fire), callbacks should be set and then variables specified (so that all callbacks fire), or objects should be established in their order in the package definition (so that their order determines which callbacks fire).
To create a packstring or packfile, enter the appropriate one of these statements:psûp.snew{obj} p.fnew{filenm; obj}The result of p.fnew is null and that of p.snew, ps, is of course a packstring, which is a simple character string.
- filenm names a file. The name given in filenm will have .pkg appended to it if it does not already contain a period. filenm is one of:
- a symbol;
- a character vector;
- a two-element nested array (flnm;opts) where flnm is a file name in one of the previous two forms and opts is a character string selecting options. Options are discussed in "Inquiry and Options", below.
- obj is either:
- the names of the global objects to be stored in the package, as a symbol vector or scalar - unqualified names being taken to be in the root context, so `b and `.b are equivalent; or
- a slotfiller, whose first element (0Øobj) supplies the names of the objects to be stored in the package and whose second element (1Øobj) supplies their values.
To establish (fix) objects in the workspace from data and definitions in a package, enter the appropriate one of these statements:p.sfix{ps; sel} p.ffix{filenm; sel}where ps and filenm are as in the previous pair of statements and sel is one of:
The explicit result of both functions is the Null.
- Null - i.e., p.sfix{ps;()} or p.sfix{ps;} or similarly for p.ffix -, meaning that everything in the package is to be established in the workspace.
- A list of names to be fixed in the workspace, given as a scalar or vector of symbols.
- A two-element nested vector of the form (pkgnames;wsnames), where the two elements are symbol scalars or vectors of the same length. The objects to be retrieved are listed in pkgnames and the names they are to be given in the workspace are listed in the corresponding positions in wsnames.
The following two functions return data from a package in the form of a slotfiller, without establishing any global objects:
sfûp.sslot{ps; sel} sfûp.fslot{filenm; sel}where the arguments are as described above, except that sel is only null or a list of names, not a two-element nested vector. In the result, sf, the first element lists the names of the objects and the second gives their values; in other words, sf is in one of the allowable forms for the right argument in the creation and modification functions.A function retrieved in this way is returned as a function scalar. Since there is no analogous form for a dependency, it cannot really be retrieved by these functions, and it has a null value in sf.
A list of the items in a packstring or packfile can be obtained by entering the appropriate one ofp.scatalog{psname} p.fcatalog{filenm}They both produce symbol vectors.
bû10+cû27 ã Set up variables b and c f{x;y}:(|x)Ó|y ã and a function f d:f{b;c} ã and a dependency d, and sûp.snew `b`c`d`f ã put them in a packstring s 'test.m'à'abcdefghijklmnop' mf1ûmfû1à'test.m' ã Create mapped file mf bû38; cû1000; ã Change values of b and c sûp.sadd{s;`b`mf} ã and change the b value in s ã and put mapped file mf in s Ø_ex¡ `b`c`d`f`mf 0 0 0 0 0 mf1[0 1 2]û'ABC' ã Change contents of file, using mf1 _ex `mf1 0 p.sfix{s;} ã Establish the objects in s d ã Recall that we didn't 38 ã change c in s mf ã mf has the latest value of the file. ABCdefghijklmnop p.sslot{s;} ã Retrieve s as a slotfiller. < `.b `.c `.d `.f `.mf < < 38 < 27 < ã Null for the dependency. < .f ã A function scalar for a function. < ABCdefghijklmnop ã The contents of the file. p.sslot{s;`b`c} ã Selective retrieval, as a < `.b `.c ã slotfiller for visibility. < < 38 < 27 ã Now demonstrate a packfile, an option string, and a slotfiller argument. p.fnew{('test.m';"vh");(`one`two`three;(3;2;1))} ã PKG: 0: Storing .one ã The v elicits messages. ã PKG: 1: Storing .two ã PKG: 2: Storing .three ã PKG: Storing hash table. ã The h creates a hash table. p.fslot{'test.m';`two`three} < `.two `.three ã Retrieved from the file. < < 2 < 1
doc@aplusdev.org | © Copyright 19952008 Morgan Stanley Dean Witter & Co. All rights reserved. |