Reading from stdin is similar in most languages – it’s represented as some kind of stream from which you can read bytes, or lines, or whatever. Sometimes you have the ability to read in a blocking vs non-blocking way.
Your example usage of readAll()
seems fine – that’s one way to do it. But it maybe isn’t the most efficient way. It reads the whole blob up front, and then split()
creates a new Array of Strings.
That’s probably why you typically see reading lines from stdin in a while loop, as you mentioned. You can also create an interactive CLI this way (or a game like Zork). You’ll also notice that the docs say it splits on “CR and/or LF bytes” - because of the different ways Win / *nix handle newlines. This is probably better if you’re expecting text. And again, more efficient to split it as you go.
Here’s a quick example of a readLine()
while loop:
class Test {
static function main() {
var line:String;
try while (true) {
line = Sys.stdin().readLine();
trace(' ---> "${ line }"');
} catch (e:haxe.io.Eof) {
trace('End of file, bye!');
}
}
}
You can either type into this (pressing CTRL-D
is the *nix friendly way to end your input), or pipe into it, like you mentioned:

Note that -x
is Haxe’s internal interpreter mode, which also supports the Sys
api and is much faster to work with than cpp
.
As for encoding, the readLine()
doc doesn’t say, but if you look at the source, it uses bytes.toString()
as well, so no difference from what you’ve done above.
Also, you may often see programmers get cryptic, doing the assignment in the while condition just to save a line:
try while ( (line = Sys.stdin().readLine()) != null) { }
Happy coding!
-Jeff