in reply to Bug with substring return value?

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.

Replies are listed 'Best First'.
Re: Re: Bug with substring return value?
by shotgunefx (Parson) on Apr 14, 2002 at 00:08 UTC
    Yeah. I realized that after a couple gentle knocks on the head from Dog_and_Pony. I was just way to tired. I swore I had 4 arguments before I wrote the post. :)

    -Lee

    "To be civilized is to deny one's nature."