lua-users home
lua-l archive

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


I've been working with a system that expects me to output binary data
to stdout, and I realized it'd be a good idea to replace print() with
a function that writes to some other file (stderr seems like a good
choice) so that it can still be used for debugging.

The problem that occurs to me is that print() does a bit of magic to
align things nicely to columns (it looks more complex than just
appending a tab to everything?) and I'm not sure how that works. If I
wrote a replacement, I'd have to try to reproduce that behaviour or
live without it.

It occurs to me that it'd be much easier if I could just write something like:
print.output_file = io.stderr
and just change a local variable or upvalue in print() instead of
having to replace the function entirely, since want to keep all of the
other logic. But presently this is not possible.

I suppose you can always define your functions as callable tables if
you want this to be possible, and with the debug library you could
probably define __index and __newindex for functions globally and use
getupvalue/getlocal to do it for arbitrary functions. But it still
wouldn't be terribly helpful here, since I doubt print() has a
local/upvalue telling what file to output to with some convenient
name. So my thought is not just "what if this were possible", but
"what if this were the default behaviour and functions were written
with it in mind?"

-- 
Sent from my Game Boy.