UPDATE: I have to agree with the rest of them. For safety reasons (so you don't demolish the test file), you may want to open $file but save to $file2 just incase the unexpected happens..
I agree, and will update my node to do so.
How exactly isn't this going to treat HTML correctly? ... It's not interpreting the file as HTML at all
That's all I meant; it won't look for HTML tags, it will look for literal text, including what it finds in comments, script, etc.
My question to you was, what exactly is line 5 doing with the joining, maping and sorting? You're playing with length which I thought only stored the length in characters of the item you're using it with.
Sorting greatest length first ensures that the match will work if you have e.g. "<A " and "<A HREF". Without the sort, you get results like:
$ perl use warnings; use strict; my @codes = ("<a ", "<a href"); my $codes_regex = join "|", map quotemeta $_, # sort { length $b <=> length $a } @codes; my $text = "testing a link: <A HREF=\"fooble.html\">boofle</a>"; print "in: $text\n"; $text =~ s/($codes_regex)/lc $1/gie; print "out: $text\n"; __END__ output with the sort: in: testing a link: <A HREF="fooble.html">boofle</a> out: testing a link: <a href="fooble.html">boofle</a> and without: in: testing a link: <A HREF="fooble.html">boofle</a> out: testing a link: <a HREF="fooble.html">boofle</a>
This is because the perl regexes prefer the leftmost |'d alternative, even if it makes a shorter match.

The map is just to apply the quotemeta; the join is to put | between tags.


In reply to Re: Re: Re: substuting a whole file by ysth
in thread substuting a whole file by Anonymous Monk

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.