Update: Sigh. Seeing your reply to RollyGuy, maybe all you want is to backwhack the slash or backslash like so:

perl -pi -e's/\{\/rtf1\}//g' /path/to/file.ini
</Update>

The basic method for that is to read the old file, make your modifications, then write the result to a either a new file or over the old one, and finally rename the new file, if used, over the old. There are several reasons to take all those steps.

The interference can happen several ways. A newly started instance may read its configuration from a half-written file. An instance may try to rewrite the configuration and find an empty file for a basis, if another instance is doing the same. These conditions are called races.

Whether you write to a copy and rename depends on whether you read the whole file into memory, or edit as a stream. Both techniques demand care with file locking.

Here is a single file technique:

use Fcntl qw( :flock ); { my $ini; open $ini, '+<', "$path/$fname" or die $!; flock $ini, LOCK_EX; my @lines = <$ini>; seek $ini, 0, 0; print $ini edit(@lines) or die $!; close $ini or die $!; }
I've assumed some sub edit is defined which returns a list of lines for the new file.

In some circumstances it may be useful to use sysopen since it permits file modes that are unavailable with open. For a stream edit, this technique is like the edit mode '-pi=bak' available from the command line:

use Fcntl; { my {$in,$out); sysopen $out, "$path/$fname.bak", O_WRONLY | O_CREAT | O_EXCL | O_EXLOCK or sleep 1 and redo; open $in, '<', "$path/$fname" or die $!; flock $in, LOCK_EX; while (<$in>) { # edit operations to modify or skip $_ print $out or die $!; } close $in; close $out or die $! rename "$path/$fname.bak", "$path/$fname" or die $!; }
Here, output file locking is done with the combination of flags in sysopen. The use of O_EXCL with rename is worth study.

I've used some Perl 5.6 -isms with lexical handles and 3-arg open, but all this can be done with global handles and two arg open, as well.

After Compline,
Zaxo


In reply to Re: file operations by Zaxo
in thread file operations by Anonymous Monk

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.