in reply to does not write to file anymore

print() and close() are also system calls just like open(). Put an or &ErrorFile( $mainpath ) on them too. You should be tracking $! as well.