1.lua的环境变量和函数
(1)_G
表 (个人习惯遍历下_G 看看当前环境支持什么库 很多库不是默认就支持的 需要按照流程导入或者加载)
一个@R_301_398@(非函数),内部储存有当前所有的全局函数和@R_301_398@的table 环境(参见 §2.2)。 Lua 自己不使用这个变量;改变这个变量的值不会对任何环境造成影响,反之亦然。(使用函数setfenv()
可以改变运行环境)
理解_G https://www.jianshu.com/p/7b8ae23ecd81
(2)_ENV表 储存这个环境(代码块)下所有能用到的全局函数和@R_301_398@ 注意在代码块定义的局部变量或者局部函数自然不会存在在这个_ENV表内 注意upvalue变量也算在里面的 用这个_ENV表可以很直观的查看 当前函数能直接调用的所有@R_301_398@函数和upvalue变量
_ENV默认会指向_G表 _ENV表的作用:表示当前代码块的环境,每一个代码块的环境都有一张_ENV
_ENV的理解 https://www.bbsmax.com/A/nAJvmlbwzr/
(3)热更新原理了解https://www.jianshu.com/p/30416db4f649
https://blog.csdn.net/zxm342698145/article/details/79654623
require 加载并执行文件代码 注意加载的参数不要后缀 并且在一个页面加载同一个文件只会加载一个
dofile 加载并执行 加载参数要有后缀 每次加载都会加载 不会合成一次
loadfile 加载但是不执行代码
loadstring 加载字符串 返回一个函数 理解:f = loadstring("i = i+1") 可理解为(但不完全是)f = function() i = i+1 end (注:这里的变量"i"是@R_301_398@,不是指局部变量,如果没有定义@R_301_398@"i",调用f()则会报错!,即loadstring不涉及词法域)
简书上lua资料真的很多
2 lua其他的内置函数的了解
小知识:简易迭代器的实现思路 理解下就好
for XX in XXX do
end
当xx为nil就会停止循环
tempTbale={10,20,128);">30,128);">40,128);">50,128);">60} --闭包思路实现一个数组迭代器 function test(T) 懒得检测是不是表了 local i=0 return () i=i+1 return T[i],i 注意这里是返回2个值 一个是数组元素的值 第二个是数组元素的key 和平时的顺序是相反的 end end f=test(tempTbale) for v,k in test(tempTbale) do这个for in 循环停止的条件是第一个参数为nil就停止 print(v,k) end
小知识:lua下访问网页源码 主要是涉及到socket库的使用 注意 https是无法访问的 http没问题
--socket 和socket.http 是什么关系 --》 socket.http 是socket的一个元素表 。
转载 https://bbs.xdow.net/thread-2826-1-1.html
http get请求
local http = require("socket.http") local res,code = http.request(http://www.baidu.com); if code == 200 then print(res,128);">0end 也可以这样 local response_body = {} http.request({ url = ,sink = ltn12.sink.table(response_body) })
获取外网ip地址 http://www.ip.cn/then local i,j = string.find(res,0);">%d+%.%d+%.%d+%.%d+) local ipaddr =string.sub(res,i,j) print(ipaddr,255);">end
返回的2个参数中,res 是 http body 的内容,也就是请求网页的内容,code 是 http 状态码, 返回200的话就表示正常返回。
如果传入的是 table 的话,就需要用一个容器来接收 http body 的内容。
http post请求
{} local post_data = ‘asd‘; res,code = http.request{ url = http://127.0.0.1/post.PHPPOST { [Content-Type"] = application/x-www-form-urlencodedContent-Length"] = #post_data,},source = ltn12.source.string(data= .. post_data),0);"> ltn12.sink.table(response_body) }
这里注意记得 method 传入 POST, 因为默认是 GET。
headers 参数,由一个 table 组成,key 为 header,value 为 header 内容。
source 参数,这里是填入 POST 的参数,多个数据的情况用 & 隔开,例如 "data1=a&data2=b"。
此代码仅为举例说明,请勿直接复制使用。
挂载代理
以socket的方式访问
local host = www.baidu.com" local file = /local sock = assert(socket.connect(host,128);">80)) 创建一个 TCP 连接,连接到 HTTP 连接的标准 80 端口上 sock:send(GET " .. file .. HTTP/1.0\r\n\r\nrepeat local chunk,status,partial = sock:receive(1024) 以 1K 的字节块接收数据 until status ~= closed sock:close() 关闭 TCP 连接
smtp方法发送mail
local smtp = socket.smtp) from = <[email protected]> 发件人 --发送列表 rcpt = { <[email protected]><[email protected]><[email protected]> { headers = { to = [email protected]", 收件人 cc = <[email protected]>‘,0);"> 抄送 subject = This is Mail Title },body = 这里放邮件内容 } r,e = smtp.send{ server = smtp.126.comsmtp服务器地址 user = [email protected]smtp验证用户名 password = ******smtp验证密码 from = from,rcpt = rcpt,source = smtp.message(mesgt) } if not r print(e,0);">); else print(发送成功!",end
net.time函数实现获取网络时间参考代码,如无特殊需要请用 net.time 函数
local socket = require socket.core server_ip =132.163.4.101132.163.4.102132.163.4.103128.138.140.44192.43.244.18131.107.1.1066.243.43.21216.200.93.8208.184.49.9207.126.98.204207.200.81.113205.188.185.33 } nstol(str) assert(str and #str == 4local t = {str:byte(1,-1)} local n = for k = n= n*256 + t[k end return n gettime(ip) local tcp = socket.tcp() tcp:settimeout(10) tcp:connect(ip,128);">37) success,time = pcall(nstol,tcp:receive()) tcp:close() return success and time or nil nettime() for _,ip in pairs(server_ip) time = gettime(ip) if time then time end print(nettime(),128);">0)
统计毫秒精度的时间,如无特殊需要请用 os.clock
小知识:lua的链表结构 和数组比较 并没有什么明显的优势 不过顺手一提
https://blog.csdn.net/thydamon/article/details/23758833
小知识:元表和元方法
主要作用是实现类的继承 基本上就是2个函数 和 几个常见的元方法
https://www.jianshu.com/p/cb945e7073a3
最基本的类继承功能的实现
zeroTable={a=2,c=3}创建一个元表 aa={11,128);">22,128);">33}普通表 zeroTable.__index=zeroTable 设置元表里面如果找不到对应索引就去后面的表去找 setMetatable(aa,zeroTable)设置zeroTable是表aa的元表 同时元表zeroTable里面的元方法也通过这个函数到了表aa里面 注意 元表的普通元素不会过去 只有特殊的元方法才会 print(aa.a)输出下表aa中不存在的索引项 也有值了
小知识 pcall函数的基本理解
1.pcall的第一个参数是function 而且不能加括号 如果这个函数内报错 去掉错误 后面的参数其实是 第一个参数函数的参数 有点绕口
2pcall的返回值是2个 第一个返回值true false 表示里面是否无错误运行了 第二个返回函数本身的返回内容 或者 错误提示信息
test3= (a) return a/2 end traceprint(test3(4))test23.lua:25: 2.0 traceprint(pcall(test3,128);">6))test23.lua:26: true 3.0 traceprint(aaaa"))test23.lua:27: false E:\lua\test23.lua:23: attempt to perform arithmetic on a string value (local ‘a‘) 个人理解的常见pcall的使用例子 status,result=bbbif status then traceprint([result]:" .. tostring(result)) else<error>:tostring(result))test23.lua:33: <error>:E:\lua\test23.lua:23: attempt to perform arithmetic on a string value (local ‘a‘) end