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 THIRD EDITION now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.