A Perl function which takes a substring of a string, returns it, and can replace it with another string.
substr($foo, offset);

returns the last part of the string $foo, beginning at the specified offset. Just like an array subscript, a negative offset counts from the end of the string.

By itself, substr($foo, offset) doesn't actually do anything. There are two ways of applying substr to do useful work.

  1. Use the return value; that is,
    $bar = substr($foo, offset)
    or
    if (substr($foo, offset) =~ /bar/)
  2. Replace the substring with something else; that is,
    substr($foo, offset) = "bar"

substr($foo, offset, length);

As above, but limits the string to a given number of characters.


substr($foo, offset, length, replacement);

Shorthand for 'substr($foo, offset, length) = replacement'