Lua package functions
Lua package functions

These are the functions in the "package" table.

The package manager is designed for scripters who need to use multiple files and load them in a consistent way.

See also the "require" and "module" functions in the Lua base functions page.




package.config

This is a string containing the various character used by the package manager to handle loading packages:

The default entries are:


\  --> directory seperator
;  --> path seperator
?  --> path mark
!  --> execution directory
-  --> ignore mark - ignore everything before it to build a function name


package.cpath

This is a string containing the search path used for loading DLLs when using the "require" function. Default values are:


.\?.dll;!\?.dll;!\loadall.dll


In the above, the package name (eg. "socket") will replace the "?" characters and the current executable pathname (eg. "c:\mushclient") will replace the "!" characters.

package.loaded

A table of the packages that are already loaded. Built-in packages (eg. table, string) are also in this table.


table.foreach (package.loaded, print)

  -->

string	table: 00301370
package	table: 00302C70
_G	table: 00300200
os	table: 003034E0
table	table: 00302560
math	table: 00304B00
coroutine	table: 00301040
debug	table: 00304340
io	table: 00303D60



package.loaders

This is a table of the loaders that Lua calls when handling the "require" function. As defined in the source code they are presently done in this order:


  • Preloader
  • Lua loader
  • DLL loader
  • All-in-one loader


You could conceivable alter the behaviour of the "require" function be re-odering the loaders (eg. load DLLs before Lua code), deleting a loader, or adding your own.

The existing loaders appear to push a string describing what files they failed to load (which you can see if you load a non-existant package), so any loaders you write yourself could return the loaded function on success, and an error string on failure.


package.loadlib (libraryname, funcname)

Loads the dynamic library (DLL), and tries to find the entrypoint named "funcname".
On success returns the function funcname.
On failure returns nil plus 2 error messages.

The error might be "The specified module could not be found." if the DLL is not found. You may also get this message if the DLL is dependant on another DLL and that other DLL cannot be found. Use a "dependency checker" to see what DLLs the target DLL might require. If you are certain that the DLL you are requesting exists, then the problem is probably a dependency.

The error might be "The specified procedure could not be found." if the function is not found in the DLL. You may also get this message if the DLL is dependant on another DLL and some entry points in that other DLL cannot be found. Use a "dependency checker" to see whether all required entry points have been resolved. If you are certain that the entry point that you are requesting exists in the target DLL, then the problem is probably a dependency from the target DLL to another DLL.

You would normally then execute the function (as in the example below) to get it to install the Lua functions into the script space.


f = assert (package.loadlib ("luacom.dll", "luacom_openlib"))
f () -- execute function: luacom_openlib


Or, more briefly:


assert (package.loadlib ("luacom.dll", "luacom_openlib")) ()



package.path

This is a string containing the search path used for loading Lua code when using the "require" function. Default values are:


.\?.lua;!\lua\?.lua;!\lua\?\init.lua;!\?.lua;!\?\init.lua


In the above, the package name (eg. "socket") will replace the "?" characters and the current executable pathname (eg. "c:\mushclient") will replace the "!" characters.

package.preload

A table of functions that can be used as loaders. If you want to make a special loader for your package you can insert it into the package.preload table. For example:


package.preload ["foo"] = function () print "loading foo" end
require "foo"  --> loading foo


In this example, when we required the package foo, the function was called.


package.seeall (module)

Sets a metatable for module with its __index field referring to the global environment, so that this module inherits values from the global environment. To be used as an option to function module. For example:


module ("foo", package.seeall)


The module code applies the function package.seeall to the new package (foo) and thus now package foo can access global variables.


See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_coroutines Lua coroutine functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_math Lua math functions
DOC_lua_os Lua os functions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions

(Help topic: general=lua_package)

DOC_contents Documentation contents page