Let me see how much I misunderstand the "less magical" code:

rename($html_file, "$html_file.bak")    or die;
Makes a copy of the file with the .bak extension

open(my $fh_in,  '<', "$html_file.bak") or die;
Opens the copy of the file for reading

open(my $fh_out, '>', $html_file) or die;
Opens the original file for writing, wiping out the contents?

    while (<$fh_in>) {
while there are file contents in the backup file, do something with them

# Keep (possibly edited) line print $fh_in $_; }

Print whatever the backup file is back into the backup file?

Ok, yeah, I still don't get this. I know I should, I've read enough examples, but I don't.
I read it as making a copy of the file I want to change, wiping out the contents of the original, modifying the copied file somehow and writing the modified text back into the copied file? Why am I not seeing this still?

Also, I don't get the while(<$fh_in>) { s/// } instead of using

my @lines = <$fh_in>; for (@lines) { s/// }
because do I want to do the s/// on the entire file at once? Don't I want to go line by line?

As for the using Perl to write PHP, as ignorant as I may seem about Perl, I am continuously frustrated with moving into PHP. What I find is that the things that PHP does easier than Perl does not outweight the things that I could do easily in Perl that cannot do easily in PHP.


I learn more and more about less and less until eventually I know everything about nothing.

In reply to Re^2: modify file in place in script? Regex for changing includes from SSI to PHP by hmbscully
in thread modify file in place in script? Regex for changing includes from SSI to PHP by hmbscully

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.