lua-users home
lua-l archive

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


On Thursday 01 December 2005 6:03 pm, Jens Alfke wrote:
> It looks as though Lua's io package is hardwired to use the ANSI
> stdio library. The reference manual  says "a file handle is a
> userdata containing the file stream (FILE*)..." [5.6] There doesn't
> seem to be a way to 'subclass' file handles the way I could e.g.
> subclass java.io.OutputStream.
>
> What I want to do is redirect Lua output to my own function, so that
> calls to print(), io.write(), io.output():write(), etc. all call my
> function so it can collect the output and do something with it.
>
> (Apologies if I'm missing something obvious; I've used a lot of
> languages but I'm a newbie at Lua.)

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

or even:

function io.write (...)
   -- your writing code
end

also, you could modify a file's metatable to change it's behavior:

getmetatable (io.output()).write = mywritefunc



-- 
Javier

Attachment: pgpLrXppq4lpk.pgp
Description: PGP signature