6.8 Perl substr — simple string handling without regex

In Perl, not every string problem needs a regular expression. If you know where something is in a string, substr is often clearer, safer, and faster.


1. Basic form

substr STRING, OFFSET, LENGTH

my $s = "PerlIsFun";
my $part = substr($s, 0, 4); # "Perl"

Offsets start at 0. The length argument is optional.


2. Omitting the length

my $rest = substr($s, 4);   # "IsFun"

If the length is omitted, substr returns everything from the offset to the end of the string.


3. Negative offsets and lengths

substr($s, -3);      # last 3 characters: "Fun"
substr($s, 0, -3);   # everything except last 3: "PerlIs"

Negative values are often more readable than equivalent regular expressions.


4. Modifying a string in place (lvalue substr)

substr($s, 0, 4) = "Ruby";
# $s is now "RubyIsFun"

substr can replace part of a string directly, without copying and without a regex.


5. substr vs regex

Using a regular expression:


my ($head, $tail) = $s =~ /(.{4})(.*)/;

Using substr:


my $head = substr($s, 0, 4);
my $tail = substr($s, 4);

When positions are known, substr is usually clearer.


6. Combining index and substr

my $line = "key:value";
my $pos  = index($line, ":");

my $key = substr($line, 0, $pos);
my $val = substr($line, $pos + 1);

For simple separators, this is often easier to read and debug than a regex.

When substr is the right tool

Use substr when:

  • positions are fixed or known
  • you only need slicing or replacement
  • a regex would be overkill

Use a regular expression when structure is variable and pattern matching is required.


7. Summary
  • substr works on positions, not patterns
  • It does not backtrack or fail mysteriously
  • It can extract and modify strings
  • It is often the simplest correct solution

Sometimes the simplest tool is the best one.

Knowing its limits: when substr is the wrong choice
  • Unicode grapheme clusters
  • variable-width encodings
  • semantic patterns