Try:
my $result = substr($string,2,6,''); print $result,"\n"; # Result is 234567
perldoc -f substr:
An alternative to using substr() as an lvalue is to specify the replacement string as the 4th argument. This allows you to replace parts of the EXPR and return what was there before in one operation, just as you can with splice().

I agree that the assignment way is pretty weird and contraintuitive, but I guess there is a reason... such as the possibility to use both ways depending on what you want to acheive?

Update: I just realized that it makes a bit more sense when you think about this:
$x = $y = 42;
Which sets first $y to 42, then $x to the value of $y. In which case, you set the substring to a value, then that value is propagated to the next variable. And when you assign a substring to an empty string, it shrinks, and then the substring from position 2 to 7 is returned (which is now what was in positions 8 to 13). Heh. Still pretty weird anyways, but there is method to the madness. :)
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

In reply to Re: Bug with substring return value? by Dog and Pony
in thread Bug with substring return value? by shotgunefx

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.