in reply to open works fine but not working

Just a shot in the dark, but is it possible that another process is clobbering the file? Maybe you should try

warn "size: ", -s $tempfile;

after the open() statement.