require函数详解(转) --------》》》 http://blog.sina.com.cn/s/blog_5a6f39cf01016yxs.html
贴原文如下
简介
require函数像dofile一样载入文件为一个Chunk并执行。但具有两个好处:1. 按模式加载文件 2.不会重复载入相同的文件
require和package.path的关系
典型的package.path值如下(其中D:Bin为lua.exe所在目录):
.?.lua;D:Binlua?.lua;D:Binlua?init.lua;D:Bin?.lua;D:Bin?init.lua
通常对我们有用的目录是.?.lua
例如require("test")将载入.test.lua
require和package.loaded的关系
如果没有载入任何Chunk,典型的package.loaded是类似如下的一张表
string
package
_G
os
table
math
coroutine table: 003738A8
debug
io
当我们require("main")成功后,该表会变回
string
package
_G
os
table
math
coroutine table: 003738B0
debug
io
main
require函数的伪代码
# function require(virname) do
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# if not anyfileloaded then
#
#
# end
载入父目录文件
值得注意的是当前目录下的子目录可以用require("subdir\subdirfile")的方式载入,而父目录却不可以用 require("..\pardirfile")的方式载入。
原因是subdirsubdirfile被替换为.subdirsubdirfile.lua;...
而..pardirfile被替换为.\\pardirfile.lua;...
我们可以通过如下方式载入父目录文件
package.path = package.path .. ";..\?.lua"
require(pardirfile)
注意这里package.path = package.path .. ";..\?.lua"和package.path = "..\?.lua;" .. package.path;有些区别;前者最后匹配";..\?.lua",后者首先匹配";..\?.lua"; 至于那种方式好那就看各位的爱好了
谨记为后期review所用