Searching Scalars

Regular expressions are nice for searching scalars for patterns, but sometimes they're overkill. In Perl, some overhead—but not much—is involved with assembling the pattern and then searching for the pattern within scalars. Also, you can easily make mistakes when writing regular expressions. Perl provides several functions for searching and extracting simple information from scalars.

Searching with index

If you merely want to find one string within another scalar, Perl provides the index function. The syntax for index is as follows:

index string, substring
index string, substring, start_position
						

The index function starts at the left of string and searches for substring. The index function returns the position at which substring ...

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.