lua-users home
lua-l archive

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


On Sun, Sep 9, 2012 at 10:03 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

> `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.

Thank you very much, Dirk. I've added that wisdom to my notes.

Best regards,

Paul