Package

package( ) or
(String packageName) or
(Package aPackage)

package() with one argument changes the current package to the specified package. package() with no argument returns the Package object of the current package.

package("foo")   ==> package "foo"
package()        ==> package "foo"
package("")      ==> package ""
package()        ==> package ""
use(String packageName)

The current package imports all functions from pkgName. Imported functions can be called without specifying the package name. Module is based on this built-in function.

package("foo")
function x() 100
y = 200

package("bar")
use("foo")
x ==> function x
y ==> error

package("")
x ==> error
y ==> error
findPackage(String packageName)

findPackage() returns the Package object of the specified name. If the package does not exist it returns null.

findPackage("foo")   ==> null
package("foo")       ==> package "foo"
package("")
findPackage("foo")   ==> package "foo"
package()            ==> package ""
getPackage(String packageName)

getPackage() returns Package object of the specified name. If it does not exist it creates one and return it.

package()             ==> package ""
findPackage("bar")    ==> null
getPackage("bar")     ==> package "bar"
package()             ==> package ""
createPackage()
createPackage( ` expression ; ... `)
createPackage( expression , ... )
# of parameters The types The action
1 java.lang.String Evaluate the string in a package
1 java.lang.Object[] Interpret each element as a [key, value] pair and define them in a package
any any If there is any function in the parameters, register the function with its name in a package.
e.g.
p = createPackage()
p.x = 1
p.y = 2

q = createPackage("x = 1; y = 2")
q.x     ==> 1
q.y     ==> 2

r = createPackage([["a", "hello"], ["b", "world"]])
r.a  ==> "hello"
r.b  ==> "world"

Unlike ordinary functions, createPackage() evaluates its parameters with the caller's context. See also "Script Package".

aPackage .defined (String symbolName , Context context )

Package.defined method checks if the specified symbol name is defined in the target package.

getPackage("foo")
context = getContext()
findPackage("foo").defined("bar", context)   ==> false
foo::bar = 100
findPackage("foo").defined("bar", context)   ==> true
aPackage .clear (String symbolName, Context context )

Package.clear method removes the specified symbol in the target package.

context = getContext()
findPackage("foo").defined("bar", context)   ==> true
findPackage("foo").clear("bar", context)
findPackage("foo").defined("bar", context)   ==> false

Persistence

package . save (OutputStream output )

Save the symbol table of the the specified package into outputStream. Compiled functions are decompiled so that they can be deserialized.

e.g.
package().save(open("/tmp/save.pkg", "w"))

This function saves only serializable objects from the symbol table of the target package.

If "pnuts.verbose" property is "true", name and value of variables to be saved are displayed.

package = readObject ( filename )
package ( package )
e.g.
package(readObject("/tmp/save.pkg"))

If "pnuts.verbose" property is "true", names and values to be read are displayed.


Back