Dofile, loadfile, require differences in LUA

Source: Internet
Author: User

1.loadfile--only compile, do not run

LoadFile Name Incredibles, it only loads the file, compiles the code, does not run the code in the file.
For example, we have a Hellofile.lua file:

The code is as follows:
Print ("Hello");
function hehe ()
Print ("Hello");
End


There is a code in this file, and a function. Try LoadFile load this file with the following code:

The code is as follows:
LoadFile ("Hellofile.lua");
Print ("End");

The output results are as follows:

The code is as follows:
[Lua-print] End


If LoadFile executes the code in the file, the hello string should be output.
The result is that it does not execute code.

2.dofile--execution

Obviously, Dofile is the guy who executes the code, the following code:

The code is as follows:
Dofile ("E:/android/wordspace_cocosiderc0/cocosluatest/src/hellofile.lua");
Print ("End");

The output results are as follows:

The code is as follows:
[Lua-print] Hello
[Lua-print] End


It's a little awkward here, the file path I used the absolute path, because Dofile uses the relative path in the Coco Code IDE to find the file (even if Addsearchpath is used)

But it doesn't matter, it doesn't affect the content of this article.

3.require--I only do it once.

Require and dofile a bit like, but very different, require the first time the file is loaded, the code will be executed.

However, after the second time, the file is loaded again and will not be repeated. In other words, it saves the files that have already been loaded and does not load repeatedly.
The test code is as follows:

The code is as follows:
For i = 1, 2, 1 do
Require ("Hellofile.lua");
End
Print ("End");

In order to illustrate this situation, I deliberately called two times require, the output results are as follows:

The code is as follows:
[Lua-print] Hello
[Lua-print] End


As we said, it was called two times, but the code was executed only once.
If this is replaced with dofile, it will output two times the Hello string.

Dofile, loadfile, require differences in LUA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.