Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening Lua Files in Interactive Mode

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?

like image 381
Pwrcdr87 Avatar asked Mar 23 '23 09:03

Pwrcdr87


1 Answers

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.

like image 198
Yu Hao Avatar answered Mar 30 '23 00:03

Yu Hao