shevek: It re-opens the same file for output.

toniax: You say you are new to Perl programming, and by implication you may be new to programming in general. If so, it is well to point out a critical side-effect of your OPed program: When the file is re-opened for output, the original content of the file is destroyed. The only place the original file content still exists is in an array in your program. If your program should fail for any reason before the original content is completely processed, the output file will be left only partly full of processed data – and possibly entirely empty.

One approach to handling a situation like this (in which processed output data is intended to be held in a file with the same name as the input file) would be:

  1. Open a temporary file (e.g., date.txt.out) and write processed data to this file. When processing is complete and the temp file is closed without error,
  2. the original input file is renamed (e.g., to date.txt.bak). If the input file rename operation is error-free,
  3. the output file is renamed to its final form (e.g., date.txt), and if this operation is successful,
  4. perhaps delete the temporarily renamed input file (although this file may instead be left behind as a .bak file).

Note that error checking is vital at each stage of this process. In this way, much grief may be avoided.


In reply to Re: regular expression by AnomalousMonk
in thread regular expression 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.