in reply to open works fine but not working
I'm sure you did this, but no one's mentioned it yet, print out $tempfile just before (and after?!?) open, and fileno RESULTS.
(The "is not a typwriter" is almost certainly from an earlier output command of some sort (or something returning its own errno which happens to match that value)).
  p