lua-users home
lua-l archive

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




On 2019-07-30 2:12 p.m., Sean Conner wrote:
It was thus said that the Great Soni They/Them L. once stated:
> since lua doesn't have refcounting and some ppl wanna do basically > io.open(file):read("*a") I wanna propose io.lines("filename", "*a")() > (or alternatively io.lines("filename", "*a", "*l")) should close the > file as if the iterator ran out.

   Lua 5.4 already does that.

[spc]lucy:/tmp>lua-54
Lua 5.4.0  Copyright (C) 1994-2019 Lua.org, PUC-Rio
> print(io.lines("tabs"))
function: 0x8b39900	nil	nil	file (0x8b410f8)

   That final value marks the descriptor for closing when the iterator is
done.  If you want to automatically close the file after reading the
contents:

	local data do
	  local <toclose> f = io.open(myfile)
	  data              = f:read("a")
	end

	-- f is closed, and data has the contents.

   -spc


no, sorry, I think you misunderstood.

io.lines("filename", "*a")()

note the call at the end. this reads the whole file. ideally it'd also close it.

compare to:

io.open("filename"):read("*a")