The grep Function

The grep function searches through a list of strings and returns an array of the strings that match a regular expression. (UNIX users will notice that this function works very much like the system grep command. Microsoft Windows users will find that it works like a very souped-up FIND command.)

The general form of the function is

grep /expression/, list
where /expression / is the regular expression to check, and list is a list of strings to examine.

For example, if you want to find any lines with the word “TODO:” in them, you can use the statement:

my @todo_lines = grep /TODO:/, @file_lines;

Get Perl for C Programmers 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.