[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: question on load(io.lines(filename, “L”))
- From: Byunghoon Kim <bkim.snu@...>
- Date: Tue, 16 Jun 2020 20:50:44 +0900
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