lualua-5.3

What does "*all" mean in `io.read("*all")`?


I know format "*a" is used to read a whole file, "*l" used to read a line. But what does "*all" (which appears in the book Programming in Lua) mean? I also saw "*line" on some webpages.

The Lua reference only specifies those one character formats, and doesn't say anything about longer formats.

At first I thought, formats with more than one characters mean reading each format individually, like "*nn" would read two numbers, "*ll" would read two lines. But that doesn't seem to be the case.

The only place I've found about this is a reddit comment say that

But "umber" is a noise string. You're relying on the implementation of file.read to ignore all characters in the format specifier after the second for star formats. What if that changes?

So what's the correct behavior of "*all" and the like, or is it really defined?

ps: I'm using Lua 5.3, but the relevant reference is same for other versions I have checked.


Solution

  • Lua does not check the whole word. It only tests the first symbol after the optional asterisk sign.

    If you look at the lua sources, the liolib.c file that has io library functions - you'll find that the first * symbol is skipped if present, it's there for compatibility with older versions only. Then the first letter after it is tested to be 'n' for the number, 'l' for the line, 'L' for the line with end-of-line, and 'a' for the whole file. It doesn't care if there are other letters in that string, you can write whatever is better for readability - as long as the first letter is in that list - it will work. But it will check for more string arguments in the read() call if you need more than one read.