Me and my very basic file writing questions again. I hope I'm not posting too many basic questions. But everyone's really been helpful and I greatly appreciate it. In response to my question remove blank lines with regex, Biker gave me this advice:
Consider this:
# Open both IN_FILE and OUT_FILE here! while(<IN_FILE>) { chomp; next unless length; print OUT_FILE "$_\n"; }
I always come back to the following:

Do not slurp in a file into an array. Someday that input file will be huge. And that will happen when used for production data. And you're on vacation. And you'll have to come in to the office during that sunny day on the beach.

Read the input file line by line and act upon each line (here by potentially writing it to the output file.)
This is obviously good advice and I want to take advantage of it. However, I'm not quite sure what the code should look like, specifically for finding and replacing all occurences of a string. Is it something like this? (Please forgive my probably-incorrect code.)
open (INFILE, "input.txt"); open (OUTFILE, ">output.txt"); while (INFILE) { $_ =~ s/foo/bar/gms; print OUTFILE "$_\n"; }
Will something like this work? If not, what will?

Thank you, again!

In reply to reading/writing line by line by amarceluk

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.