lua-users home
lua-l archive

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


You can also do this to avoid generating closures each time:


local gsub = (function()
	local ref = setmetatable({}, {__mode='v'})
	local f = function(...) return ref.userdata(...) end

	return function(s, pat, repl)
		if type(repl) == 'userdata' then
			ref.userdata = repl
			repl = f
		end
		print(s, pat, repl)
		return s:gsub(pat,repl)
	end
end)()