In the perlfunc:substr docs, I can find only two references to what it returns:

and

The latter isn't really relevant as I'm using the 3-arg version, but it does describe one of the two behaviours I would have expected.

If the 3-arg version as an lvalue were consistant with the 4-arg version, I would expect the replaced part, ('quick ' in my short demo program below) to be returned.

If it was consistant with assignment, then I would expect either the thing being assigned (''), or the result of the assignment ('the brown fox');

What is actually being returned is 'brown '

What is happening is that the substitution (deletion) is being performed using the offset and length, AND THEN the same values are being re-used to extract a return value FROM THE RESULTANT of the assignment.

Whilst I can explain what is happening, using the offset and length *twice* is at least strange. Doing so in order to provide a return value that bears no relationship to either the sub-string specified by the programmer, nor the resultant of the assignment simply make no sense at all.... leastwise, not to me.

#! perl -w my $s = qq(the quick brown fox); print substr($s, 4, 6) = '', $/; __END__ #output C:\test>191356 brown C:\test>

With respect to if '' and undef are lvalues, then this is a feature, and not a bug

You rightly point out that the docs say

You can use the substr() function as an lvalue, in which case EXPR must itself be an lvalue.

However,

substr EXPR,OFFSET,LENGTH,REPLACEMENT

substr EXPR,OFFSET,LENGTH

substr EXPR,OFFSET

The EXPR that must be a LVALUE is the first parameter to substr, not the value being assigned to it.

I can explain what is happening. I still contend that it is neither documented that way nor intuatively expected. Nor, in my opinion, is it sensible.

As it is, I am sufficiently convinced that this is a bug that I will send a perlbug report in along with the bulk of this post minus references about our debate. A link to this entire thread, and allow them to decide whether this is a documentation change, and code fix or simple a vagary of "BrowserUk's missunderstanding".


What's this about a "crooked mitre"? I'm good at woodwork!

In reply to Re: Re: [substr] anomaly or mine? by BrowserUk
in thread [substr] anomaly or mine? by BrowserUk

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.