Q&A

Q1: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 ...

Get Sams Teach Yourself Perl in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.