Are substr , index , and rindex really necessary? Why do they exist when regular expressions can be used for most of those operations?
A1:
First, regular expressions for simple string searches are generally slower than index and rindex . Second, writing substitution expressions for fixed character positions with regular expressions can be messy; substr is a much more elegant solution sometimes. Third, Perl is a rich language. Use what you like; you have plenty of choices.
Q2:
What happens with substr (or index or rindex ) if I specify an index that's beyond the end of the scalar?
A2:
One nice thing about computers is that they're consistent and have limitless amounts of patience. With questions like "What happens if I ?" sometimes just trying it is easiest ! What's the worst that can happen?
In this case, accessing a portion of a scalar that doesn't exist might cause a use of undefined value error if you have warnings enabled, as you should. For example, if you use $a="Foo"; substr($a, 5); , the substr function returns undef .