in reply to getc() question

The problem is that you are testing the truth of $_ which does not tell you if a character was read.

In perl the false values are 0, "0", "" and undef. If any of these values are returned, you exit the loop, while if you read the docs for getc, the value returned when there are no characters to read is always undef which can be differenciated from the other falses by testing with the defined function instead of basic truth.

So you want to turn the while line to:
while( defined($_=getc(STDIN)) ){