In my mind, this is a documentation bug. If you match instead of substitute, you'll get the expected behavior. Also, the substitution does, in fact, substitute exactly what you ask it to.

I'm grateful that this question led me to spend a couple hours re-reading perlre et al but I never did find anything that explained this behavior. This could easily be an oversight on my part.

To return to the original question, I don't think this is either a bug or a feature but, rather, a design decision: when replacing multiple times, should $1 contain the value of the last successful match or should it be undef to reflect the fact that the last match failed? I wouldn't presume to say that the wrong choice was made but it's obvious what that choice was.


In reply to Re: Bug or feature? s/// and the g option by rowdog
in thread Bug or feature? s/// and the g option by Belgarion

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.