I think this is the same anomaly as I noticed at [substr] anomaly or mine?.

I raised a perlbug (I can't find the ticket number off hand). The response I got (nearly a year later:) was that it was, at best, a documentation bug, and not something that needed to be fixed. I disagreed quite strongly with this.

I don't have a problem with the two versions doing something different if it was clearly documented, then you could make your choice of which form to use dependant upon which result fits your needs.

My concern was more that the result of the lvalue version produces a result that is neither one thing nor t'other.

$s='foobar'; print '4-arg: ', substr( $t=$s,1,4,$_) , ' 3-arg: ', substr($t=$s,1,4)=$_ for qw[ a ab abc abcd abcde abcdef ]; 4-arg: ooba 3-arg: ar 4-arg: ooba 3-arg: abr 4-arg: ooba 3-arg: abcr 4-arg: ooba 3-arg: abcd 4-arg: ooba 3-arg: abcd 4-arg: ooba 3-arg: abcd

As you can see, the 4-arg results are consistent, but the 3-arg version varies wildly.

  1. If the value being assigned is shorter than the section being replaced, the result is the length of the assignment, but its value is
    1. the value of the assigned value
    2. PLUS a random character from the unaffected part of the original value...in this case the last character. </li
  2. If the value being assigned is the same length as the section being replaced then the result is the value being assigned.
  3. If the value being assigned is longer than the section being replaced, then the result is the first N characters of the value being assigned, where N is the length of the section being replaced.

No one is ever going to convince me that simply documenting this behaviour will make that logical, predicable or useful......still. "The problem space is a mess" :)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail


In reply to Re: An Oddity of substr by BrowserUk
in thread An Oddity of substr by Zaxo

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.