Lua 语言的常见的相关base 变量和base函数

Lua 语言的常见的相关base 变量和base函数

global variable: _VERSION

该变量存的是当前的Lua解释器版本信息的字符串。
ex:

print(_VERSION)

assert 函数

原型: v = assert(v, message)
其中, 当v的值不是nil或者不是false(即为true), 则返回v, 否则, 返回message。 message默认设置为 “assertion failed!”。

assert (5 == 6, "oh no!") --> error: oh no!

dofile函数

原型: val = dofile(filename)
该函数执行一个Lua 语言写的脚本file。 返回的值是该函数返回的值。
ex:

dofile ("myfile.lua") 

使用dofile等同于如下:

function dofile (filename)
  local f = assert (loadfile (filename))
  return f ()
end -- dofile

ipairs函数

原型: it, t, 0 = ipairs(t)
解释: 上述函数用于对一个numerically keyed table进行迭代遍历。
例子:

for i, v in ipairs (t) do
  -- process loop here (i is the key, v is the value)
end -- for

ex:


t = {
    "the",
    "quick",
    "brown",
    "dog",
   }

for key, value in ipairs (t) do
  print (key, value)
end -- for

运行结果输出:

1 the
2 quick
3 brown
4 dog

error函数

原型:error (message, level)
解释:该函数会raise 一个error 信息。
level = 1: 当前函数错误
level = 2: parent function出错。
…。

ex:

error ("Insufficient funds") --> error raised: "Insufficient funds"

loadstring函数

原型: f = loadstring (str, debugname)
解释: 对a sting of lua code进行编译。该函数会对lua 代码串 进行parse, 返回编译后的chunk 作为函数, 但是并不执行。 如果未成功, 则返回nil和一个错误信息。


f = assert (loadstring ("print 'hello, world'"))
f ()   --> hello, world

或者直接如下语句,从而避免中间变量:

assert (loadstring ("print 'hello, world'")) () --> hello, world

loadfile函数

原型: f, err = loadfile (filename)
解释: load一个lua文件并进行parse。
例子:


f = assert (loadfile ("myfile.lua"))
f () -- execute function now

require函数

原型:val = require (modname)
解释: 载入(load)一个module。
ex:

f = require ("test")  --> loads module "test" 

xpcall

原型:ok, result = xpcall (f, err)
发生错误, 调用err, ok = false, result是err返回的值。
正确, 则ok = true, result是require返回的值。

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。

    例子:

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.

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值