in reply to A Better Way

Be aware that the way you've written the code is unsound in that it offers the potential for permanent (and possibly silent) data loss. Consider what happens if your:

print XFIL $line;
fails. You're not checking print's return value, so you may not even be aware that it's failed. If it fails, your var.txt file will be corrupted. If you don't have a backup copy, you will have suffered permanent data loss.

Apart from the possibility of print failing (due to disk full or disk quota exceeded, for example), suppose your program gets interrupted during the print loop (via CTRL-C or other signal, OS restart, or a power failure, for instance). Because it was interrupted, your program had not finished writing the var.txt file. So when you now re-run your program, you will read (and write) an incomplete var.txt file. And again, you may not be immediately aware that you've (permanently) lost data.

I assume Tie::File will deal sensibly with these sort of routine robustness and reliability issues. If you don't use Tie::File, you can make your program more robust by:

  1. Write the output file to a new temporary file.
  2. Only after carefully checking that the temporary file has indeed been written without error, rename the temporary file to var.txt. (Note that the rename should be atomic on Unix systems, so there is minimal risk of data loss).

Update: See also Perl Best Practices book: is this one a best practice or a dodgy practice?.

Replies are listed 'Best First'.
Re^2: A Better Way
by toniax (Scribe) on Nov 04, 2010 at 22:53 UTC
    Hello eyepop,
    I am aware of that. That is my next item I will be working on.Thanks for the suggestion and the book link.
    This program I am working on seems never ending, but I do not mind. I am starting to get addicted to Perl for some strange reason.