Lua 语言的常见的相关base 变量和base函数
2015-09-07 11:45
796 查看
Lua 语言的常见的相关base 变量和base函数
Lua 语言的常见的相关base 变量和base函数global variable _VERSION
assert 函数
dofile函数
ipairs函数
error函数
loadstring函数
loadfile函数
require函数
xpcall
pcall
module函数
global variable: _VERSION
该变量存的是当前的Lua解释器版本信息的字符串。ex:
[code]print(_VERSION)
assert 函数
原型: v = assert(v, message)其中, 当v的值不是nil或者不是false(即为true), 则返回v, 否则, 返回message。 message默认设置为 “assertion failed!”。
[code]assert (5 == 6, "oh no!") --> error: oh no!
dofile函数
原型: val = dofile(filename)该函数执行一个Lua 语言写的脚本file。 返回的值是该函数返回的值。
ex:
[code]dofile ("myfile.lua")
使用dofile等同于如下:
[code]function dofile (filename) local f = assert (loadfile (filename)) return f () end -- dofile
ipairs函数
原型: it, t, 0 = ipairs(t)解释: 上述函数用于对一个numerically keyed table进行迭代遍历。
例子:
[code]for i, v in ipairs (t) do -- process loop here (i is the key, v is the value) end -- for
ex:
[code] t = { "the", "quick", "brown", "dog", } for key, value in ipairs (t) do print (key, value) end -- for
运行结果输出:
[code]1 the 2 quick 3 brown 4 dog
error函数
原型:error (message, level)解释:该函数会raise 一个error 信息。
level = 1: 当前函数错误
level = 2: parent function出错。
…。
ex:
[code]error ("Insufficient funds") --> error raised: "Insufficient funds"
loadstring函数
原型: f = loadstring (str, debugname)解释: 对a sting of lua code进行编译。该函数会对lua 代码串 进行parse, 返回编译后的chunk 作为函数, 但是并不执行。 如果未成功, 则返回nil和一个错误信息。
[code] f = assert (loadstring ("print 'hello, world'")) f () --> hello, world
或者直接如下语句,从而避免中间变量:
[code]assert (loadstring ("print 'hello, world'")) () --> hello, world
loadfile函数
原型: f, err = loadfile (filename)解释: load一个lua文件并进行parse。
例子:
[code] f = assert (loadfile ("myfile.lua")) f () -- execute function now
require函数
原型:val = require (modname)解释: 载入(load)一个module。
ex:
[code]f = require ("test") --> loads module "test"
xpcall
原型:ok, result = xpcall (f, err)发生错误, 调用err, ok = false, result是err返回的值。
正确, 则ok = true, result是require返回的值。
[code]function f () return "a" + 2 -- will cause error end -- f function err (x) print ("err called", x) return "oh no!" end -- err print (xpcall (f, err)) --> err called [string "Immediate"]:2: attempt to perform arithmetic on a string value false oh no! function f2 () return 2 + 2 end -- f print (xpcall (f, err)) --> true 4 function f () return "a" + 2 end -- f print (xpcall (f, debug.traceback)) --> false stdin:2: attempt to perform arithmetic on a string value stack traceback: stdin:2: in function `f' [C]: in function `xpcall' stdin:1: in main chunk [C]: ?
pcall
原型:ok, result [ , result2 …] = pcall (f, arg1, arg2, …)解释: 调用函数 in a protected mode
成功的话, 返回:
true
function results - may be more than one
失败的话, 返回:
false
error message。
例子:
[code]function f (v) return v + 2 end -- f a, b = pcall (f, 1) print (a, b) --> true 3 a, b = pcall (f, "a") print (a, b) --> false stdin:2: attempt to perform arithmetic on local `v' (a string value)
module函数
原型: module (name, ···)解释: 创建lua module。
例子:
下例主要展示如何创建一个名字为’foo’的module。实际操作中, 你可能将’test’函数的内容放在另一个单独的文件中, 然后运行质量: require “test”。
The nice thing about this approach is that nothing inside the module will “pollute” the global namespace, excepting the module name itself (foo in this case). Internally inside the module functions can call each other without having to use the package name (eg. add could call subtract without using foo.subtract).
You can make a “private” function inside the “foo” package by simply putting “local” in front of the function name.
[code]function test () local print = print --> we need access to this global variable module "foo" --> create the module now function add (a, b) return a + b end -- add function subtract (a, b) return a - b end -- subtract function hello (s) print ("hello", s) end -- hello end -- function test test () -- install module foo.hello ("world") --> hello world print (foo.add (2, 3)) --> 5 print (foo.subtract (7, 8)) --> -1 print (package.loaded["foo"]) --> table: 003055F0 print (foo) --> table: 003055F0 for k, v in pairs (foo) do print (k, v) end -- for --> _M table: 003055F0 _NAME foo _PACKAGE hello function: 00305810 subtract function: 00305760 add function: 00305780
相关文章推荐
- mac 安装lua
- Lua:编译安装LuaJIT,第一个Lua程序
- Lua:几个名词,Lua、JIT、LuaJIT
- lua类对象创建继承和使用2
- Lua图文混排
- Lua4.0中的函数
- Lua 的模式匹配
- Lua5.0 语法分析
- string evaluated instead to freemarker.template.SimpleScalar
- UPYUN 的 ngx_lua 应用实践 » 社区 » Ruby China
- Lua 基本语法
- tengine+lua实现时时在线图片缩放,剪切。 - epiclight的colorful world - 开源中国社区
- FPN/lua-sdk-for-UPYUN - GitCafe
- Tengine + Lua + GraphicsMagick 实现图片自动裁剪/缩放 - 腾勇的个人页面 - 开源中国社区
- cloudflare的新waf,用Lua实现的 - 囧囧有神的专栏 - 博客频道 - CSDN.NET
- tengine+lua实现时时在线图片缩放,剪切。 - epiclight的colorful world - 开源中国社区
- sphinx下的lua客户端 - IT博客汇
- 生产环境中 Ngx_lua 使用技巧和应用的范例 - 推酷
- 让lua支持base64和md5 - xu_ya_fei的专栏 - 博客频道 - CSDN.NET
- 使用Lua和OpenResty搭建验证码服务器 - JavaChen Blog