当前位置:网站首页>require、loadfile、dofile、load、loadstring
require、loadfile、dofile、load、loadstring
2022-07-28 00:15:00 【The senming Gang is bigger than the black tiger gang】
1.loadfile Compile only , Not running loadfile(filename, mode, env) Used to load a specified path filename Code block for , The parameters are as follows :
Basic and load equivalent , The difference is the code block used to load a file , See below :
LuaFunc.lua( To be accessed ) file :
--LuaFunc.lua
function add(x, y)
return x + y
end
m = "LuaFunc.m"
local n = "n"
print("------------------ < LuaFunc.lua > ------------------")
print("m = "..m)
print("n = "..n)
print("------------------ < LuaFunc.lua > ------------------")
The calling code is as follows :
-- Load and compile code
f = assert(loadfile("Child/LuaFunc.lua"))
-- function
f()
print("add : "..add(100, 88))
print(m)
The output is as follows :
------------------ < LuaFunc.lua > ------------------
m = LuaFunc.m
n = n
------------------ < LuaFunc.lua > ------------------
add : 188
LuaFunc.m
loadfile:Compile only , Not running , It only loads files , Compile code , Will not run the code in the file ,loadfileOnly... Will be loaded(load)luaFile but will not execute(run). such as , We have ahellofile.luafile :
print(“hello”);
function hehe()
print(“hello”);
end
There is a code in this file , And a function . Try it out loadfile Load this file , The following code :
loadfile("hellofile.lua");
print("end");
The output is as follows :
end
if loadfile If you can execute the code in the file , that , It should output hello A string of . It turns out that , It will not execute code .
2.dofile perform dofile(filename) Used to load a specified path filename Code block for , And run . The parameters are as follows :
Internal call first loadfile, Then it will run automatically , See below :
dofile("Child/LuaFunc.lua")
print("add : "..add(100, 88))
print(m)
The output is the same as the above example .
- perform
dofileIs to execute code ,dofile And requiresimilar , But it didn'tcachingFiles that have been executed once , So it can be repeated many times . Obviously ,dofile Is the guy who can execute code , The following code :
dofile("D:/vscode lua Code /hello world/foo.lua");
print("end");
The output is as follows :
hello
end
3.require I only do it once
require(modname) Used to load a specified code block , And run . The parameters are as follows :
This function will only be loaded once for the same code block ( It will be saved after loading ), and dofile similar , It will also run automatically after loading and compiling , See below :
-- Load compile and run
require("Child/LuaFunc") -- Equate to :require("Child.LuaFunc")
print("add : "..add(100, 88))
print(m)
The output is the same as the above example .
require and dofileIt's kind of like , But it's very different ,requireWhen loading the file for the first time , Will execute the code inside . however , After the second time , Load the file again , Will not be repeated . let me put it another way , It will save the loaded file , Does not load repeatedly .requireThe return value of will be storedcacheget up , So a file can only be executed once at most , Even ifrequireMany times .
for i = 1, 2, 1 do
require("foo.lua");
end
print("end");
To illustrate this situation , I deliberately called twice require, The output is as follows :
hello
end
As we said , Called twice , But the code only executes once . If you change here to dofile, Will output twice hello character string .
4.load Used to load string code blocks
Same as loadstring,5.2 After the version loadstring Removed , use load Instead of ,load(chunk, chunkname, mode, env) Used to load string code blocks , The parameters are as follows :
Pay attention here :load Used to load string code blocks and compile , But it didn't run . Remember to run it before using , See below :
a = 100
-- Load string code block
f = assert(load("a = a + 100"))
print("before a = "..a)
-- Run code block
f()
print("after a = "..a)
The output is as follows :
before a = 100
after a = 200
You can see that after loading the code block , Before running ,a The value of does not change to 100, Executed after running a = a + 100,a = 200.
5. Code block chuck How to understand lua Code block in chunk, When actually compiling , It is equivalent to being wrapped by a variable parameter anonymous function , And will bind a unique upvalue value _ENV, After running, the code block chunk Global variables in are added to Global environment variable _ENV in . Code block “a = a + 100” Equivalent to the following function :
-- Code block
function chuck()
a = a + 100
end
-- This sentence is a metaphor load The loading and compiling process of code blocks ( Not necessarily appropriate , Just think about it )
f = chuck()
-- function
f()
There may be some doubts here ,load Why can this code block in access external variables a?
env: It is an environment variable , You can customize ( It is generally used to simulate a sandbox environment ), The default value will be lua The global environment variable of _ENV fill .
_ENV yes lua5.2 Version is used to replace _G A global variable of (_ENV Inside, it points to _G Of , You can simply think that they are the same thing ), But it's a upvalue value .(_G And _ENV Those who are interested can learn by themselves ) Here is a brief description of _ENV, every last lua Code blocks have one by default _ENV Global environment variable , It's a watch , It contains all of lua Standard class libraries and all compiled global variables .
a = 100
-- Load and run the string code block
assert(load("a = a + 11; b = 'b126'; print('this is chunk')"))()
print("a = "..a)
print("_ENV.a = ".._ENV.a)
print("b = "..b)
print("_ENV.b = ".._ENV.b)
Output is as follows :
this is chunk
a = 111
_ENV.a = 111
b = b126
_ENV.b = b126
You can see :
Block of code , You can call
printfunction , This is because the defaultenvWhat's coming in is_ENV, Equivalent to calling_ENV.print.aand_ENV.aSame output , In fact, when compiling , Global variables are preceded by_ENV. limit.Compile and run code blocks
chunkafter , VariablebHas been added to_ENVin .
Custom environment variables env Use : It is generally used to simulate a sandbox environment , See below :
a = 100
-- Custom environment variables
local env = {
a = 8,
print = print
}
-- Load and run the string code block
assert(load("a = a + 10; print('this is chunk, a = '..a)", "chunkTest", "bt", env))()
print("a = "..a)
Output is as follows :
this is chunk, a = 18
a = 100
You can see the code block
chunkTestin ,aTake the custom environment variableenvOfavalue , Printa = 18;Because there is no default global environment variable
_ENVfill , So external variablesaIt hasn't been modified :a = 100env Medium print = print, Equivalent to :print = _ENV.print.
边栏推荐
- 元宇宙办公,打工人的终极梦想
- BUU-CTF basic rsa
- Glory launched a number of products at the same time. The price of notebook magicbook V 14 starts from 6199 yuan
- R语言R原生plot函数和lines函数的主要参数说明、解析(type、pch、cex、lty、lwd、col、xlab、ylab)
- 【Objective-C语言的SEL对象】
- [Development Tutorial 9] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - heart rate monitoring
- ESP8266-----MQTT云下设备上云
- [NPUCTF2020]EzRSA
- 2022/7/26
- How to use C WinForm to copy files and display progress
猜你喜欢
![[GWCTF 2019]BabyRSA1](/img/31/6727fd04be13ddd6bd46969fe2c50f.png)
[GWCTF 2019]BabyRSA1

BUUCTF-RSA4

Cache与MMU管理

JS 事件传播 捕获阶段 冒泡阶段 onclick addEventListener

4小时定单破20000+,自称“百万内最豪华”,国产品牌飘了?
![[unity] mapping 2D coordinates to ID](/img/e8/e6e56ba63dd56009bfabe647f2e3ed.png)
[unity] mapping 2D coordinates to ID

liux常用命令(查看及其开放防火墙端口号+查看及其杀死进程)
![[flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)](/img/75/509db67a580dd4b9849bea08845cc7.png)
[flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)

【开发教程9】疯壳·开源蓝牙心率防水运动手环-心率监测

New media content output method - short video
随机推荐
[sel object of Objective-C language]
It was dog days, but I was scared out of a cold sweat: how far is the hidden danger of driving safety from us?
BUUCTF-RSA
Senior how to determine the standard of software test completion
传奇服务端:GOM GeeM2引擎更新时必须要修改哪些地方?
BUUCTF-Baby RSA
The latest ijcai2022 tutorial of "figure neural network: foundation, frontier and application"
Window function over
BUU-CTF basic rsa
[NCTF2019]babyRSA1
【读书会第13期】音视频文件的封装格式
Latex中如何加粗字体 & 如何打出圆圈序号
JS ATM output
Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further
How to use C WinForm to copy files and display progress
Construction and application of super large scale knowledge map of ants
JS promotion: array flattening in JS
2022 latest Tiktok live broadcast monitoring full set of monitoring (V) product details monitoring
[ACTF新生赛2020]crypto-aes
How to use FTP to realize automatic update of WinForm