writer.pl fix

fixed typos and variable definition.  
added safety measures to inbound messages (#666)  
global definition of file_handler (for use in subroutines)