question on load(io.lines(filename, “L”))

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

question on load(io.lines(filename, “L”))

Byunghoon Kim
Hi,

I just wanted a minor clarification.

Programming in Lua, fourth edition, pp.123-124, says
   “As an example, the next call is equivalent to loadfile:
    f = load(io.lines(filename, “*L”))”

If I understand correctly, this is no longer the case with Lua 5.4. Right?

In Lua 5.4, io.lines() now returns four values - iterator function, nil, nil, to-be-closed file handle - to conform to the new semantics of generic for loop. load(), however, hasn’t been updated, and still expects four arguments; chunk, chunkname, mode, env.

Hence, if I call load(io.lines(..)), **to-be-closed file handle** from io.lines() gets wrongly assigned to **env** argument of load().

I was a bit surprised with this behavior during my self-study. Wouldn’t it be nice if this is listed in the reference manual, section 8 Incompatibilities with the previous version?

Thank you for your time.


Below is what I did:

test.lua
print(“hello world”)


Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> f = load(io.lines(“test.lua”, “L”))
> f()
hello world


Lua 5.4.0 Copyright (C) 1994-2020 Lua.org, PUC-Rio
> f = load(io.lines(“test.lua”, “L”))
> f()
(load):1: attempt to call a nil value (global ‘print’)
.....

Regards,
Byunghoon

Sent from my iPad