Question

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?

Was it helpful?

Solution

Assume that your file is named foo.lua, then in the Lua interpreter (i.e, the interactive mode), use loadfile. Note that loadfile does not raise error, so it's better to use assert with it.

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

It will load the chunk in foo.lua into the function f. Note that this will only load the chunk, not run it. To run it, call the function:

f()

If you need to run it immediately, you can use dofile:

dofile("foo.lua") 

Lua uses package.path as the search path which gets its default value from LUA_PATH. However, it's better to use proper relative path in practice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top