lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2012/9/10 marbux <marbux@gmail.com>:
> I do not know enough about Lua's innards to distinguish between "load"
> executing the string itself or "load" merely presenting the string to
> one or more other lua methods to process. With no informed knowledge,
> I describe the *result* as "using load to inject a string into the
> function and execute the string." That is how my lame brain perceives
> the result.
>
> Or put more simply, I don't know *why* it works but I know that it does work.

`load(str)` delays execution.  If its result is not immediately assigned
or executed, for all practical purposes `load` does nothing except test
that `str` is a valid chunk of Lua code.

`load(str)()` is almost exactly the same as injecting the contents of
`str` at that point of the program except that the code in `str` sees
global variables only.

> local x=1234; load("a=x")(); print(a)
nil
> local x=1234; a=x; print(a)
1234

`myfunc=load(str)` is almost exactly the same as injecting
   "myfunc=function() "..str.." end"
at that point of the program except that if you do it with load,
myfunc doesn't have any upvalues.

> local x=1234; myfunc=load("a=x"); x=5678; myfunc(); print(a)
nil
> local x=1234; myfunc=function() a=x end; x=5678; myfunc(); print(a)
5678

The term `upvalue` is unfortunate.  As this example shows, `upname` would
have been less misleading.