Welcome, brother whiteperl :-)

Is there a way to edit text files without reading the entire file into an array?

Well, it's a matter of context. As you will learn reading the fine documents other monks pointed you to, reading from a filehandle could be done in list context, e.g.: my @file_contents = <MYFILE>;; or in scalar context, e.g.: my $line = <MYFILE>.

When you read from a file in list context, you get all the file as a list (i.e.: something you could assign to an array or give as argument to a function that requires a list, like print). Assigning to an array slurps all the file into the array, one line per element. OK, you already know this.

When you read in scalar context, you get next line from the file. The concept of "next line" should be quite clear: if you open a file and immediately after you do a my $firstline = <MYFILE> ;, $firstline gets the first line. Next <MYFILE> "call" in scalar context will get the second line and so on.

Whenever possible, it is better not to read a whole file in memory at once since it could eat a lot of memory if the file is big, and it could take a lot of time, too. Reading step by step, like while (my $line = <MYFILE>) { do_something_with($line) } is often better.

Ciao!
--bronto

Update: of course, do_something_with($line) could end up in messing up $line and printing out to another filehandle, which is what I understand you call editing :-)

# Another Perl edition of a song:
# The End, by The Beatles
END {
  $you->take($love) eq $you->made($love) ;
}


In reply to Re: Editing a text file without reading it into an array by bronto
in thread Editing a text file without reading it into an array by whiteperl

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.