lua-users home
lua-l archive

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

On 1 Dec '05, at 3:15 PM, Javier Guerra wrote:

it's not obvious if you're used to other languages, but in Lua you can just 

redefine any function; in other words, the io package is just a table with 

functions, and, like any table, can be modified:

io.write = mywritefunc

OK. Then my next question is, what are the core bottlenecks that output goes through? In other words, what's the minimal set of functions I have to replace? There seem to be many different ways to write to stdout:
From the reference, it looks like print() calls io.stdout():write(), and io.write() calls io.output:write().

also, you could modify a file's metatable to change it's behavior:
getmetatable (io.output()).write = mywritefunc

Would it work to create my own 'object' (table) and add a 'write' method to it, then make that the default output file? That sounds like it should work, unless there is code inside the Lua VM that knows that files are userdata and gropes the FILE* pointers out of them.

Something like:

myout = {
write = function(self,str) ..... end
print("hello world")