Lua dofile loadfile loadstring difference, loadfileloadstring
Dofile, which is treated as a primitive operation of the chunk that Lua runs the code.
1. Dofile is actually a helper function. The function that truly completes the function is loadfile;
2. Unlike dofile, loadfile compiles the code into an intermediate code and returns the compiled chunk as a function without executing the code;
3. Loadfile does not throw an error message but returns an error code.
Change Print. lua in TestMain to otherPrint. lua (The lua file does not exist). The result is as follows: loadfile returns the error code and error message, but dofile throws an exception, so it does not Print ---------- B -----------
4. Loadfile only needs to be compiled once and can be run multiple times; dofile needs to be compiled every time (applicable to simple functions ).
Based on the above four items, we can consider loadfile as defined in this way:
Lua treats each chunk as an anonymous function:
Suppose we have a file foo. lua:
After the command f = loadfile ("foo. lua") is executed, foo is compiled but not defined yet. to define it, run chunk:
Loadstring is similar to loadfile, but it does not read chunk from the file, but from a string;
Loadstring compilation does not care about the lexical scope:
In this example, g uses the local variable I as imagined, but f uses the global variable I; loadstring is always compiled in the global environment.
Lua loadfile function problems
Loadfile is only searched in the specified path. If it is not an absolute path, it is the relative path to the current directory.
Require searches by package. path.
Lua, for the problem of setting the file path in lua
Er, the lua syntax does not contain these interfaces. The system provides only require, dofile, loadfile, and loadstring Loading modules or chunk interfaces, the interfaces you provided are obviously self-encapsulated interfaces. You have to look at the code by yourself. But the literal meaning of the function is actually quite clear. Load the role data, load configuration files and resources?