in Perl in a Nutshell it states... "You can also specify a replacement string in the fourth parameter to replace the substring. The original extracted substring is still returned."

The Nutshell book is right, but you don't use the fourth parameter. You use three parameters, and use substr as an lvalue that can be assigned to.

# Begin of assignment substr( # Start of substr call, using substr as lvalue $string, # First argument 2, # Second argument 6 # Third argument ) # Anything after this is not part of the substr call = # Assignment operator ''; # Righthand operand of the assignment operator # End of assignment. An assignment returns the assigned value, # '' in this case.
What you want is:
substr( # Start of substr call, returning the old stuff $string, # First argument 2, # Second argument 6, # Third argument '' # Fourth argument: replacement text );
You could have found out by reading substr's entry in perlfunc, which begins with all ways of calling it:
substr EXPR,OFFSET,LENGTH,REPLACEMENT
substr EXPR,OFFSET,LENGTH
substr EXPR,OFFSET

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.


In reply to Re: Bug with substring return value? by Juerd
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.