LoadFile, Dofile, require detailed in Lua _lua

Source: Internet
Author: User
Tags lua

Http://www.jb51.net/article/55125.htm

Should not have discussed these functions today, however, in order to gather the word number. No, in order to facilitate the future of the article, or to talk about the basic functions of the ~

1.loadfile--only compiles, does not run

LoadFile name incredible, it will only load files, compile code, will not run the code in the file.
For example, we have a Hellofile.lua file:
Copy code code as follows:
Print ("Hello");
function hehe ()
Print ("Hello");
End

This file has a code, and a function. Try LoadFile load this file with the following code:
Copy code code as follows:
LoadFile ("Hellofile.lua");
Print ("End");

The output results are as follows:
Copy code code as follows:
[Lua-print] End

If LoadFile will execute the code in the file, it should output the Hello string.
The results show that it does not execute code.

2.dofile--execution

Obviously, Dofile is the guy who executes the code, the following code:
Copy code code as follows:
Dofile ("E:/android/wordspace_cocosiderc0/cocosluatest/src/hellofile.lua");
Print ("End");

The output results are as follows:
Copy code code as follows:
[Lua-print] Hello
[Lua-print] End

Here's a little awkward, file path I used the absolute path, because dofile in the Coco Code IDE using the relative path will not find the file (even if the use of Addsearchpath)

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 in the first time to load the file, will execute the code inside.

However, the second time after the file is loaded again, it will not be repeated. In other words, it saves files that have already been loaded and does not load repeatedly.
The test code is as follows:
Copy code code 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 of the results are as follows:
Copy code code as follows:
[Lua-print] Hello
[Lua-print] End

As we said, the call was made two times, but the code was executed only once.
If this is replaced with Dofile, the Hello string will be output two times.

4. End

I found that it is a bit slow to finish the article while reading. It's a question of fish and paws.

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.