Thoughts on Perl and Emacs, technology and writing

Read Stream of Input With Perl

Take a look at this. I’m simulating a process streams unbuffered output output irregularly. First it sends "hello ", then waits 5 seconds and sends "world".

I run the producer on its own, just in case it is buffering the output despite the fact I told it not to by setting $|. Nope, it isn’t. On the command line, I can see the hello output and then a few seconds later the world.

The consumer outputs whatever it receives. I get ctime() from Time::localtime for brevity.

Okay, quite cool, but there is a better solution. sysread is unbuffered. You will probably have to buffer the input yourself to ensure you have enough to process, but at least you get it as soon as it arrives. (Mental note: I should check if AnyEvent has something cool for this for anything more than a quick one-off script).