And maybe if you're using a Unix/Linux environment you can do the first part of this task
the shell ... simply piping the stats program's input into the STDIN of another script that say does a
... which output is then piped (by the main script) into the actual CSV-conversion (Perl?) routine. This pushes a lot of the complexity of the task
the domain of Perl and into an environment which is really set up to do this sort of thing. A "pure-Perl solution" is not the only way to do it. Worth considering.