Hello Monks,

As the title suggests, I wanted to see if there was anyway to issue only one open command, and be able to capture ALL the
file's contents, then overwrite the file... Is this possible?

I know using ">" or write will clobber/remove any contents in the file once you issue the open command with '>' as the
mode. And "<" or read opens the file in read mode. And, doing ">>" will open in append mode to write to the end of the
file... I also know you can add "+" to any of those symbols above to add the opposite mode on top of what you already
used. *i.e. ">+" write+read, "+<" read+write, "+>>" append+read, etc....

So I'm curious if there is a special way that I can read/capture the contents of a file first, then overwrite that same
file while ONLY open'ing the file once..? Is that possible?

In case your wondering what my goal is, this is what I'm trying to accomplish: Basically, the file in question will be a
log file. And I really only want the file to contain 21 lines (*line 1 is the column headings). So lets say the file has
21 lines currently in it. What I want to do is Remove the top line of data (*NOT the header line), so remove line 2. Then
add a new line of new data to the end of the file, sort of like rotating the file's contents.

I know I can accomplish this by opening the file in read mode, slurping it's contents into an array then adding the new
line of data to that array, close the file (*the Array would contain 22 elements at this point). Then, re-open the file in
write mode (*clobber contents), loop through the array and print each element back to that same file while skipping
element [1]...

Any thoughts or suggestions would be much appreciated!

Thanks in Advance,
Matt


In reply to Capture Contents AND Overwrite without Opening Twice? by mmartin

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.