4

I am beginning to learn Lua on my own with basically no prior programming knowledge. I understand the basics of types, functions, tables, etc. But in following the Lua tuts at Lua.org, I'm currently on the "Modules Tutorial" and am having issues understanding the proper/easiest way to call a file made into interactive mode.

If I used Notepad++ or Scite to create a file, can someone please help me understand how to open said file using the proper nomenclature to open it?

4

1 回答 1

7

假设你的文件名为foo.lua,那么在 Lua 解释器(即交互模式)中,使用loadfile. 请注意,loadfile这不会引发错误,因此最好assert与它一起使用。

f = assert(loadfile("foo.lua"))

它会将块加载foo.lua到函数f中。请注意,这只会加载块,而不是运行它。要运行它,请调用函数:

f()

如果您需要立即运行它,您可以使用dofile

dofile("foo.lua") 

Luapackage.path用作搜索路径,其默认值来自LUA_PATH. 但是,在实践中最好使用正确的相对路径。

于 2013-09-17T15:37:21.800 回答