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?.


In reply to Re: A Better Way by eyepopslikeamosquito
in thread A Better Way by toniax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.