lua-users home
lua-l archive

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




To be honest though, the only time in the past
that I have had to change the way print works
was on a platform for which there was no stdout.
In that case I changed the C implementation so
that print wrote to a log file.

Perhaps if you think of io.write as "print" (in the meaning of other languages you have used), and "print" as "debugprint", then this would seem perfectly normal rather than unusual.

Quoting the Lua manual:

---cut here---

print (...)

Receives any number of arguments, and prints their values to stdout, using the tostring function to convert them to strings. print is not intended for formatted output, but only as a quick way to show a value, typically for debugging.

---cut here---

In other words, print simply sends stuff to stdout (in the parent process sense of stdout), and always to stdout.

The io library writes to user-selectable file descriptors, either implict or explicit. The implicit file descriptors happen to start off set to stdin and stdout, but can be changed.

In other words, io.write() _is_ affected if you use io.output() to redirect the io library's default output file away from stdout. Note that redirecting the io library _is not equivalent to redirecting stdout_. It simply changes the implicit file descriptors in the io library.

So print() is _not_ affected by calls to io.output() -- this is both by design and as documented.

This is rather useful. It means you have a simple and debug-friendly way of printing data which isn't affected by the way you do your "real" I/O with the dedicated io library. (You can also switch off any left-over debugging output in your code simply with something like 'print = function() end', _without_ needing to muck around with stdout in any way.)

A bit like OutputDebugString() in Windows, for example. (Actually, not really like OutputDebugString at all, but I hope this makes the point :-)