So there's really two questions here.


The first is why wasn't COW used for matching half of the code.

COW is normally used for the copy for $& and friends, but it wasn't used in this case because there's not enough space in the buffer.

CUR = 100 LEN = 101

I think two bytes are needed. One for the NUL, and one for the COW reference count. Replace $_ .= "x"; with chop;, and you get:

SV = PV(0x5deca7958ee0) at 0x5deca7994d68 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x5deca7998850 "xxxxxxxxxx"...\0 CUR = 98 LEN = 101 SV = PV(0x5deca7958ee0) at 0x5deca7994d68 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x5deca7998850 "xxxxxxxxxx"...\0 CUR = 98 LEN = 101 COW_REFCNT = 1 SV = PV(0x5deca7958ee0) at 0x5deca7994d68 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x5deca797ad20 "aaa"\0 CUR = 3 LEN = 16 COW_REFCNT = 1

The second question is why does the scalar get a new buffer for the substitution.

I'm guessing the buffer of the scalar is stolen rather than copied for $& and friends, since the expectation is that it will change.

That means the scalar always gets a new buffer.


In reply to Re^5: 5.42: Does m// toss a string around? by ikegami
in thread 5.42: Does m// toss a string around? 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.