Opening Lua Files in Interactive Mode

3.5k views Asked by At

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?

1

There are 1 answers

6
Yu Hao On BEST ANSWER

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.