模块管理:
The package library provides basic facilities for loading modules in Lua. It exports one function directly in the global environment: require. Everything else is exported in a table package.
require (modname)
require returns the value stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.
module (name [, ···])
Creates a module. If there is a table in package.loaded[name], this table is the module.Otherwise creates a new table t and sets it as the value of the global name and the value of package.loaded[name].
This function also initializes t._NAME with the given name,
Finally, module sets t as the new environment of the current function and the new value of package.loaded[name], so that require returns t.
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.
摘自:http://blog.163.com/hbu_lijian/blog/static/126129153201422902256778/
1.dofile与loadfile
dofile当作Lua运行代码的chunk的一种原始的操作。dofile实际上是一个辅助的函数。真正完成功能的函数是loadfile;与dofile不同的是loadfile编译代码成中间码并且返回编译后的chunk作为一个函数,而不执行代码;另外loadfile不会抛出错误信息而是返回错误代。我们可以这样定义dofile:
function dofile (filename)
local f = assert(loadfile(filename))
return f()
end
如果loadfile失败assert会抛出错误。loadfile更加灵活。在发生错误的情况下,loadfile返回nil和错误信息,这样我们就可以自定义错误处理。另外,如果我们运行一个文件多次的话,loadfile只需要编译一次,但可多次运行。dofile却每次都要编译。
loadstring (string [, chunkname])
Similar to load, but gets the chunk from the given string.
To load and run a given string, use the idiom
assert(loadstring(s))()
When absent, chunkname defaults to the given string.
Lua5.2:部分变化:
Function module is deprecated. It is easy to set up a module with regular Lua code.
Functions setfenv and getfenv were removed, because of the changes in environments.
增加 bit32模块
The package library provides basic facilities for loading modules in Lua. It exports one function directly in the global environment: require. Everything else is exported in a table package.
require (modname)
require returns the value stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.
module (name [, ···])
Creates a module. If there is a table in package.loaded[name], this table is the module.Otherwise creates a new table t and sets it as the value of the global name and the value of package.loaded[name].
This function also initializes t._NAME with the given name,
Finally, module sets t as the new environment of the current function and the new value of package.loaded[name], so that require returns t.
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.
摘自:http://blog.163.com/hbu_lijian/blog/static/126129153201422902256778/
1.dofile与loadfile
dofile当作Lua运行代码的chunk的一种原始的操作。dofile实际上是一个辅助的函数。真正完成功能的函数是loadfile;与dofile不同的是loadfile编译代码成中间码并且返回编译后的chunk作为一个函数,而不执行代码;另外loadfile不会抛出错误信息而是返回错误代。我们可以这样定义dofile:
function dofile (filename)
local f = assert(loadfile(filename))
return f()
end
如果loadfile失败assert会抛出错误。loadfile更加灵活。在发生错误的情况下,loadfile返回nil和错误信息,这样我们就可以自定义错误处理。另外,如果我们运行一个文件多次的话,loadfile只需要编译一次,但可多次运行。dofile却每次都要编译。
loadstring (string [, chunkname])
Similar to load, but gets the chunk from the given string.
To load and run a given string, use the idiom
assert(loadstring(s))()
When absent, chunkname defaults to the given string.
Lua5.2:部分变化:
Function module is deprecated. It is easy to set up a module with regular Lua code.
Functions setfenv and getfenv were removed, because of the changes in environments.
增加 bit32模块