what is wrong with this perl code ? STDOUT / STDERR issue ?
I expect the code to do this:
- immediately print command line parameter 0 and 1
- run the for loop
- print "test2" forever
what happens on the console is this
- first it waits (for loop)
- then it prints $ARGV ....
- then it runs the while loop
if I let it print to STDERR instead of STDOUT it does what I want, except that I don't want it to print to STDERR... I just don't understand what the heck is going on!
Ran into this a while ago myself. I don't know the ins and outs, but this has to do with perl keeping stuff in a buffer before actually releasing/printing it.
I solved it this way:
sneaky bastard that perl, should've consulted the cookbook fist ;-)
By default, OSes and langs tend to buffer a certain amt of data before sending to stdout.
This buffering speeds up the program and lessens disk wear 'n tear.
As shown above, you can force no-buffering in Perl if you need to.
I tend to use that for logfiles, as it's usually the last msg befoe a prog breaks that tells you why, but this msg 'can' be lost if the prog fails to flush the buffer before die-ing.
(Actually, I rarely use the 'die' cmd. I always try to do a controlled exit that closes files/ db handles and logs/emails error, but that only works for anticipated errors, Sometimes things just die...)
|All times are GMT -5. The time now is 12:59 PM.|