There are two ways, with '+>' and '+<'. They are distinctly different and are used in different situations.

'+>' truncates the opened file, just like '>' does. That means the contents of the file before opening are lost. You can seek to a previous location and read/modify what you have written there. [ I've never needed to use this one, either. I'd suspect a design flaw if I did ]

'+<' is more like '<', open to read. The contents of the file are not clobbered, and the file position is set to the beginning. You can read the file and even modify it (if you preserve length or don't care about it). Having read to the end, you can freely append to it.

The commonest situation where you want to open r/w happens when the file must be flock'd to prevent races on its content. '+<' permits you to read and modify the file under a single lock.

For either, the seek, tell, and truncate builtins are often needed.

After Compline,
Zaxo


In reply to Re: Using +> for File Read/Write by Zaxo
in thread Using +> for File Read/Write by slloyd

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.