in reply to Function skips

writeFiles function only works once

That is not your problem.

open FILE, "+>", $filename or die $!;

The file mode you are using will overwrite the previous contents of the file.    You need to use append mode which will not overwrite the previous contents:

open FILE, '>>', $filename or die $!;