require
Lua function

require

Summary

Loads a module

Prototype

val = require (modname)



Description

Loads the named module.

First checks the table package.loaded to see if it has already been loaded. If so, returns the value there.

Otherwise it tries to find a loader for the module. It tries these things:


  • If the table item package.preload [modname] is a function, that is called as the module loader.

  • Process the variable package.path, substituting modname where it has a "?" (that is, ? becomes the name) and then repeatedly attempting to open the files in the list (paths separated by semicolons) as Lua source files. The first attempt in the default configuration is in the current directory, for modname.lua, then in the executable directory (that is, where MUSHclient executable is, if you are running from MUSHclient), various combinations of subdirectories and file names. To see exactly what tests are being done, just type 'require "foo"' and the error message will show you.

  • Process the variable package.cpath, substituting modname where it has a "?" (that is, ? becomes the name) and then repeatedly attempting to open the files in the list (paths separated by semicolons) as DLLs. This attempts to load various DLLs, again see the package.cpath variable to see which ones exactly, or try a dummy require, as described above. If a DLL is found it attempts to call the function "luaopen_" concatenated with the module name, where dots are replaced by underscores.

  • Finally if all else fails try to load the all-in-one loader (executable-path/loadall.dll) and look for an appropriate function.


Once a loader is found, require calls the loader with a single argument, modname. If the loader returns any value, require assigns the returned value to package.loaded [modname]. If the loader returns no value and has not assigned any value to package.loaded [modname], then require assigns true to this entry. In any case, require returns the final value of package.loaded [modname].


f = require ("test")  --> loads module "test" 


Effectively this lets various modules "require" a package, and it will only be loaded once. Also see the description for "module" for ways of effectively setting up a module to work in conjunction with "require".



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_bc Lua bc (big number) functions
DOC_lua_bit Lua bit manipulation 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_package Lua package functions
DOC_lua_rex Lua PCRE regular expression functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions
DOC_lua_utils Lua utilities
DOC_scripting Scripting

Lua functions

LUA_assert assert (Asserts that condition is not nil and not false)
LUA_collectgarbage collectgarbage (Collects garbage)
LUA_dofile dofile (Executes a Lua file)
LUA_error error (Raises an error message)
LUA_gcinfo gcinfo (Returns amount of dynamic memory in use)
LUA_getfenv getfenv (Returns the current environment table)
LUA_getmetatable getmetatable (Returns the metatable for the object)
LUA_ipairs ipairs (Iterates over a numerically keyed table)
LUA_load load (Loads a chunk by calling a function repeatedly)
LUA_loadfile loadfile (Loads a Lua file and parses it)
LUA_loadlib loadlib (Loads a DLL (obsolete in Lua 5.1))
LUA_loadstring loadstring (Compiles a string of Lua code)
LUA_module module (Creates a Lua module)
LUA_next next (Returns next key / value pair in a table)
LUA_pairs pairs (Traverse all items in a table)
LUA_pcall pcall (Calls a function in protected mode)
LUA_print print (Prints its arguments)
LUA_rawequal rawequal (Compares two values for equality without invoking metamethods)
LUA_rawget rawget (Gets the value of a table item without invoking metamethods)
LUA_rawset rawset (Sets the value of a table item without invoking metamethods)
LUA_select select (Returns items in a list)
LUA_setfenv setfenv (Sets a function's environment)
LUA_setmetatable setmetatable (Sets the metatable for a table)
LUA_tonumber tonumber (Converts a string (of the given base) to a number)
LUA_tostring tostring (Converts its argument to a string)
LUA_type type (Returns the type of a variable)
LUA_unpack unpack (Unpacks a table into individual items)
LUA_xpcall xpcall (Calls a function with a custom error handler)

(Help topic: lua=require)

DOC_contents Documentation contents page