> a=1
> load("a=a+1 print(a)") ()
2
> f=load("a=a+1 print(a)")
> f()
3
load("a = a+1")
效果"等同"于
function () a = a+1 end
> do
>> a = 100
>> local a = 1
>> f1 = function () a=a+1 print("1:" .. a) end
>> f1()
>> f2 = load("a=a+1 print(a)")
>> f2()
>> end
1:2 -- f1输出本地变量的值
101 -- f2输出全局变量的值
注意, load和loadfile期望文本为chunk内容, 如果是表达式的话, 可在表达式前添加return返回表达式的值.
> f = load("a")
> print (f)
nil
> f()
stdin:1: attempt to call global 'f' (a nil value)
stack traceback:
stdin:1: in main chunk
[C]: in ?
改成 :
> f = load("return a") -- 返回表达式的值
> f()
> print(f())
nil
load和loadfile只是预加载代码, 并不会执行它, 只有执行了这个匿名函数后, 里面的代码才会得以执行.
> f1 = load("f2 = function() a=99 print(a) end")
> f2() -- 直接调用f2是不行的, 因为f1还没有执行.
stdin:1: attempt to call global 'f2' (a nil value)
stack traceback:
stdin:1: in main chunk
[C]: in ?
> f1() -- f1被调用后, f2函数就存在了.
> f2()
99
> f3 = load("b=0") -- b的定义同样要在f3函数调用后才会被执行
> print(b)
nil
> f3()
> print(b)
0
load和loadfile不检查文本的内容, 如果非法的话, 返回nil, 同时输出错误信息.
> print(load("err"))
nil [string "err"]:1: syntax error near <eof>
> f = load("err")
> print(f)
nil
> assert( load("err") )
stdin:1: [string "err"]:1: syntax error near <eof>
stack traceback:
[C]: in function 'assert'
stdin:1: in main chunk
[C]: in ?
如果正确则返回参数值(匿名函数)
> f = assert( load("a=1 print(a)") )
> print(f)
function: 0x204eae0
> f()
1
> assert(nil)
stdin:1: assertion failed!
stack traceback:
[C]: in function 'assert'
stdin:1: in main chunk
[C]: in ?
> assert(nil,"nihao")
stdin:1: nihao
stack traceback:
[C]: in function 'assert'
stdin:1: in main chunk
[C]: in ?
> = assert(1,"nihao")
1 nihao
> a = assert(1,"nihao")
> print(a)
1
dofile是对loadfile封装后调用这个匿名函数 :
function dofile (filename)
local f = assert(loadfile(filename))
return f()
end
> do
>> print ("enter function to be plotted (with variable 'x'):")
>> local l = io.read()
>> local f = assert(load("local x = ...; return " .. l))
>> for i=1,20 do
>> print( string.rep("*", f(i)) )
>> end
>> end
enter function to be plotted (with variable 'x'):
x -- 输入x
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
*******************
********************
load (ld [, source [, mode [, env]]])
Loads a chunk.
If ld
is a string, the chunk is this string. If ld
is a function, load
calls it repeatedly to get the chunk pieces. Each call to ld
must return a string that concatenates with previous results. A return of an empty string, nil, or no value signals the end of the chunk.
If there are no syntactic errors, returns the compiled chunk as a function; otherwise, returns nil plus the error message.
If the resulting function has upvalues, the first upvalue is set to the value of env
, if that parameter is given, or to the value of the global environment. (When you load a main chunk, the resulting function will always have exactly one upvalue, the _ENV
variable (see §2.2). When you load a binary chunk created from a function (see string.dump
), the resulting function can have arbitrary upvalues.)
source
is used as the source of the chunk for error messages and debug information (see §4.9). When absent, it defaults to ld
, if ld
is a string, or to "=(load)
" otherwise.
The string mode
controls whether the chunk can be text or binary (that is, a precompiled chunk). It may be the string "b
" (only binary chunks), "t
" (only text chunks), or "bt
" (both binary and text). The default is "bt
".
loadfile ([filename [, mode [, env]]])
Similar to load
, but gets the chunk from file filename
or from the standard input, if no file name is given.
assert (v [, message])
Issues an error when the value of its argumentv
is false (i.e.,
nil
or
false
); otherwise, returns all its arguments.
message
is an error message; when absent, it defaults to "assertion failed!"