lua-users home
lua-l archive

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


On Jan 12, 2010, at 1:39 PM, Daniel Silverstone wrote:

> On Tue, Jan 12, 2010 at 03:44:33PM -0200, Roberto Ierusalimschy wrote:
>> 'loadin' is like 'load'; it accepts a reader function as argument. So
>> there is no need to have the code as string at all.
> 
> Gotcha, thanks.

It is relatively trivial to write a "loadfilewrappedin" function that incorporates this change:

function loadfilewrappedin(env, filename)
	local source;
	local function loader()
		if source then
			return source:read() or "\nend"
		else
			source = io.open(filename)
			return "in ... do\n"
		end
	end
	local function release(...)
		source:close()
		return ...
	end
	return release(loadin(env, loader, filename))
end

A "loadfilein" function that doesn't include the wrapper is even more trivial.