Opening Lua Files Interactively

I'm starting to learn Lua on my own, basically, no prior programming knowledge. I understand the basics of types, functions, tables, etc. But, following Lua tuts on Lua.org, I am currently participating in the Tutorial module, and I am having trouble understanding the correct / easy way to invoke a file made interactively.

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

+4
source share
1 answer

Suppose your file is named foo.lua , then in the Lua interpreter (i.e. interactively) use loadfile . Please note that loadfile does not cause an error, so it is better to use assert with it.

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

It will load the piece in foo.lua into the function f . Please note that this will only load the piece, not run it. To start 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 the default value from LUA_PATH . However, in practice, it is better to use the correct relative path.

+7
source

Source: https://habr.com/ru/post/1502642/


All Articles