- 8.1 What Is a Regular Expression?
- 8.2 Expression Modifiers and Simple Statements
- 8.3 Regular Expression Operators
- 8.4 What You Should Know
- 8.5 What's Next?
8.3 Regular Expression Operators
The regular expression operators are used for matching patterns in searches and for replacements in substitution operations. The m operator is used for matching patterns, and the s operator is used when substituting one pattern for another.
8.3.1 The m Operator and Matching
The m operator is used for matching patterns. The m operator is optional if the delimiters enclosing the regular expression are forward slashes (the forward slash is the default) but required if you change the delimiter. You may want to change the delimiter if the regular expression itself contains forward slashes (e.g., when searching for birthdays, such as 3/15/93, or pathnames, such as /usr/var/adm).
Table 8.1. Matching Modifiers
Modifier |
Meaning |
i |
Turn off case sensitivity. |
m |
Treat a string as multiple lines. |
o |
Compile pattern only once. Used to optimize the search. |
s |
Treat string as a single line when a newline is embedded. |
x |
Permit comments in a regular expression and ignore whitespace. |
g |
Match globally; i.e., find all occurrences. Return a list if used with an array context, or true or false if a scalar context. |
Example 8.12
1 m/Good morning/ 2 /Good evening/ 3 /\/usr\/var\/adm/ 4 m#/usr/var/adm# 5 m(Good evening) 6 m'$name'
Explanation
- The m operator is not needed in this example, since forward slashes delimit the regular expression.
- The forward slash is the delimiter; therefore, the m operator is optional.
- Each of the forward slashes in the search path is quoted with a backslash so it will not be confused with the forward slash used for the pattern delimiter—a messy approach.
- The m operator is required because the pound sign (#) is used as an alternative to the forward slash. The pound sign delimiter clarifies and simplifies the previous example.
- If the opening delimiter is a parenthesis, square bracket, angle bracket, or brace, then the closing delimiter must be the corresponding closing character, such as m(expression), m[expression], m<expression>, or m{expression}.
- If the delimiter is a single quote, then variable interpolation is turned off; in other words, $name is treated as a literal.
Example 8.13
(The Script) 1 while(<DATA>){ 2 print if /Betty/; # Print the line if it matches Betty } 3 _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) Betty Boop
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line after the _ _DATA_ _ token is read in and assigned to $_.
- All lines that match the pattern Betty are matched and printed.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
Example 8.14
(The Script) 1 while(<DATA>){ 2 print unless /Evich/; # Print line unless it matches Evich } 3 _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line from under the _ _DATA_ _ token is read in and assigned to $_.
- All lines that don't match the pattern Evich are printed.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
Example 8.15
(The Script) 1 while(<DATA>){ 2 print if m#Jon# # Print the line if it matches Jon } 3 _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) Jon DeLoach
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line following the _ _DATA_ _ token is read in and assigned to $_.
- The m (match) operator is necessary because the delimiter has been changed from the default forward slash to a pound sign (#). The line is printed if it matches Jon.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
Example 8.16
(The Script) 1 while(<DATA>){ 2 print if m(Karen E); # Print the line if it matches Karen E } 3 $name="Jon"; 4 $_=qq/$name is a good sport.\n/; 5 print if m'$name'; 6 print if m"$name"; 7 _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) 2 Karen Evich 5 <No output> 6 Jon is a good sport.
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line below the _ _DATA_ _ token is read in and assigned to $_.
- The m (match) operator is necessary because the delimiter has been changed from the default forward slash to a set of opening and closing parentheses. Other pairs that could be used are square brackets, curly braces, angle brackets, and single quotes. If single quotes are used, and the regular expression contains variables, the variables will not be interpolated. The line is printed if it matches Karen E.
- The scalar $name is assigned Jon.
- $_ is assigned a string including the scalar $name.
- When the matching delimiter is a set of single quotes, variables in the regular expression are not interpolated. The literal value $name is not found in $_; therefore, nothing is printed.
- If double quotes enclose the expression, the variable $name will be interpolated. The string assigned to $_ is printed if it contains Jon.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
The g Modifier—Global Match
The g modifier is used to cause a global match; in other words, all occurrences of a pattern in the line are matched. Without the g, only the first occurrence of a pattern is matched. The m operator will return a list of the patterns matched.
The i Modifier—Case Insensitivity
Perl is sensitive to whether characters are upper- or lowercase when performing matches. If you want to turn off case sensitivity, an i (insensitive) is appended to the last delimiter of the match operator.
Special Scalars for Saving Patterns
The $& special scalar is assigned the string that was matched in the last successful search. &` saves what was found preceding the pattern that was matched, and &' saves what was found after the pattern that was matched.
Example 8.19
1 $_="San Francisco to Hong Kong\n"; 2 /Francisco/; # Save 'Francisco' in $& if it is found 3 print $&,"\n"; 4 /to/; 5 print $`,"\n"; # Save what comes before the string 'to' 6 /to\s/; # \s represents a space 7 print $', "\n"; # Save what comes after the string 'to' (Output) 3 Francisco 5 San Francisco 7 Hong Kong
Explanation
- The $_ scalar is assigned a string.
- The search pattern contains the regular expression Francisco. Perl searches for this pattern in the $_ variable. If found, the pattern Francisco will be saved in another special scalar, $&.
- The search pattern Francisco was successfully matched, saved in $&, and printed.
- The search pattern contains the regular expression to. Perl searches for this pattern in the $_ variable. If the pattern to is matched, the string to the left of this pattern, San Francisco, is saved in the $` scalar (note the backquote).
- The value of $` is printed.
- The search pattern contains the regular expression to\s (to followed by a space; \s represents a space). Perl searches for this pattern in the $_ variable. If the pattern to\s is matched, the string to the right of this pattern, Hong Kong, is saved in the $' scalar (note the straight quote).
- The value of & ' is printed.
The x Modifier—The Expressive Modifier
The x modifier allows you to place comments within the regular expression and add whitespace characters (spaces, tabs, newlines) for clarity without having those characters interpreted as part of the regular expression; in other words, you can express your intentions within the regular expression.
Example 8.20
1 $_="San Francisco to Hong Kong\n"; 2 /Francisco # Searching for Francisco /x; 3 print "Comments and spaces were removed and \$& is $&\n"; (Output) 3 Comments and spaces were removed and $& is Francisco
Explanation
- The $_ scalar is assigned a string.
- The search pattern consists of Francisco followed by a space, comment, and another space. The x modifier allows the additional whitespace and comments to be inserted in the pattern space without being interpreted as part of the search pattern.
- The printed text illustrates that the search was unaffected by the extra spaces and comments. $& holds the value of what was matched as a result of the search.
8.3.2 The s Operator and Substitution
The s operator is used for substitutions. The substitution operator replaces the first regular expression pattern with the second. The delimiter can also be changed. The g modifier placed after the last delimiter stands for global change on a line. The return value from the s operator is the number of substitutions that were made. Without it, only the first occurrence of the pattern is affected by the substitution.
The special built-in variable $& gets the value of whatever was found in the search string.
Table 8.2. Substitution Modifiers
Modifier |
Meaning |
e |
Evaluate the replacement side as an expression. |
i |
Turn off case sensitivity. |
m |
Treat a string as multiple lines.a |
o |
Compile pattern only once. Used to optimize the search. |
s |
Treat string as single line when newline is embedded. |
x |
Allow whitespace and comments within the regular expression. |
g |
Replace globally; i.e., find all occurrences. |
Example 8.22
(The Script) 1 while(<DATA>){ 2 s/Norma/Jane/; # Substitute Norma with Jane 3 print; } 4 _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) Steve Blenheim Betty Boop Igor Chevsky Jane Cord Jon DeLoach Karen Evich
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line after the _ _DATA_ _ token is read in and assigned to $_.
- In lines where $_ contains the regular expression Norma, the substitution operator, s, will replace Norma with Jane for the first occurrence of Norma on each line. (Similar to vi and sed commands for UNIX.)
- Each line will be printed, whether or not the substitution occurred.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
Example 8.23
(The Script) 1 while($_= <DATA>){ 2 print if s/Igor/Ivan/; # Substitute Igor with Ivan } 3 _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) Ivan Chevsky
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line following the _ _DATA_ _ token is read in and assigned to $_.
- In lines where $_ contains the regular expression Igor, the substitution operator, s, will replace Igor with Ivan for the first occurrence of Igor on each line. Only if the substitution is successful will the line be printed.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
Changing the Substitution Delimiters
Normally, the forward slash delimiter encloses both the search pattern and the replacement string. Any nonalphanumeric character following the s operator can be used in place of the slash. For example, if a # follows the s operator, it must be used as the delimiter for the replacement pattern. If pairs of parentheses, curly braces, square brackets, or angle brackets are used to delimit the search pattern, any other type of delimiter may be used for the replacement pattern, such as s(John) /Joe/;
Example 8.24
(The Script) 1 while(<DATA>){ 2 s#Igor#Boris#; # Substitute Igor with Boris 3 print; } 4 _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) Steve Blenheim Betty Boop Boris Chevsky Norma Cord Jon DeLoach Karen Evich
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line after the _ _DATA_ _ token is read in and assigned to $_.
- The delimiter following the s operator has been changed to a pound sign (#). This is fine as long as all three delimiters are pound signs. The regular expression Igor is replaced with Boris.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
Example 8.25
(The Script) 1 while(<DATA>){ 2 s(Blenheim){Dobbins}; # Substitute Blenheim with Dobbins 3 print; } 4 _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) Steve Dobbins Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line following the _ _DATA_ _ token is read in and assigned to $_.
- The search pattern Blenheim is delimited with parentheses and the replacement pattern, Dobbins, is delimited with forward slashes.
- The substitution is shown in the output when it is printed. Blenheim is replaced with Dobbins.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
The g Modifier—Global Substitution
The g modifier is used to cause a global substitution; that is, all occurrences of a pattern are replaced on the line. Without the g, only the first occurrence of a pattern on each line is changed.
Example 8.27
(The Script) # With the g option 1 while(<DATA>){ 2 print if s/Tom/Christian/g; # All occurrences of Tom on each # line are replaced with Christian } 3 _ _DATA_ _ Tom Dave Dan Tom Betty Tom Henry Tom Igor Norma Tom Tom (Output) Christian Dave Dan Christian Betty Christian Dick Christian Igor Norma Christian Christian
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line after the _ _DATA_ _ token is read in and assigned to $_.
- With the g option, the substitution is global. Every occurrence of Tom will be replaced with Christian for each line that is read.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
The i Modifier—Case Insensitivity
Perl is sensitive to upper- or lowercase characters when performing matches. If you want to turn off case sensitivity, an i (insensitive) is appended to the last delimiter of the match or substitution operator.
Example 8.29
(The Script) 1 while(<DATA>){ 2 print if s/igor/Daniel/i; # Substitute igor with Daniel } 3 _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) Daniel Chevsky
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line after the _ _DATA_ _ token is read in and assigned to $_. Each time the loop is entered, the next line following _ _DATA_ _ is assigned to $_ until all the lines have been processed.
- The regular expression in the substitution is also caseinsensitive, owing to the i option. If igor or Igor (or any combination of upper- and lowercase) is matched, it will be replaced with Daniel.
- The DATA filehandle gets its data from the lines that follow the _ _DATA_ _ token.
The e Modifier—Evaluating an Expression
On the replacement side of a substitution operation, it is possible to evaluate an expression or a function. The search side is replaced with the result of the evaluation.
Example 8.31
(The Script) # The e modifier 1 $_=5; 2 s/5/6 * 4 - 22/e; 3 print "The result is: $_\n"; 4 $_=1055; 5 s/5/3*2/eg; 6 print "The result is: $_\n"; (Output) 3 The result is: 2 6 The result is: 1066
Explanation
- The $_ scalar is assigned 5.
- The s operator searches for the regular expression 5 in $_. The e modifier evaluates the replacement string as a numeric expression and replaces it with the result of the arithmetic operation, 6* 4 – 22, which results in 2.
- The result of the evaluation is printed.
- The $_ variable is assigned 1055.
- The s operator searches for the regular expression 5 in $_. The e modifier evaluates the replacement string as a numeric expression and replaces it with the product of 3*2; i.e., every time 5 is found, it is replaced with 6. Since the substitution is global, all occurrences of 5 are replaced with 6.
- The result of the evaluation is printed.
Example 8.32
(The Script) 1 $_ = "knock at heaven's door.\n"; 2 s/knock/"knock, " x 2 . "knocking"/ei; 3 print "He's $_; (Output) He's knock, knock, knocking at heaven's door.
Explanation
- The $_ variable is the string knock at heaven's door.\n;
- The s operator searches for the regular expression knock in $_. The e modifier evaluates the replacement string as a string expression and replaces it with knock x 2 (repeated twice) and concatenates (the dot operator) with the string knocking, ignoring case.
- The resulting string is printed.
Example 8.33
(The Script) # Saving in the $& special scalar 1 $_=5000; 2 s/$_/$& * 2/e; 3 print "The new value is $_\.n"; 4 $_="knock at heaven's door.\n"; 5 s/knock/"$&," x 2 . "$&ing"/ei; 6 print "He's $_"; (Output) 3 The new value is 10000. 6 He's knock,knock,knocking at heaven's door.
Explanation
- The $_ scalar is assigned 5000.
- The search string, 5000, is stored in the $& variable. In the replacement side the expression is evaluated; in other words, the value of $& is multiplied by 2. The new value is substituted for the original value. $_ is assigned the new value.
- The resulting value is printed.
- The $_ scalar is assigned the string knock at heaven's door.\n.
- If the search string (knock) is found, it is stored in the $& variable. In the replacement side, the expression is evaluated. So, the value of $& (knock) is replicated twice and concatenated with $& ing (knocking). The new value is substituted for the original value. $_ is assigned the new value and printed.
8.3.3 Pattern Binding Operators
The pattern binding operators are used to bind a matched pattern, substitution, or translation (see tr in Appendix A) to another scalar expression. In the previous examples, pattern searches were done implicitly (or explicitly) on the $_ variable, the default pattern space. That is, each line was stored in the $_ variable when looping through a file. In the previous example, the $_ was assigned a value and used as the search string for the substitution. But what if you store a value in some variable other than $_?
Instead of
$_ = 5000;
you would write
$salary = 5000;
Then if a match or substitution is performed on $salary instead of
print if /5/; or s/5/6;
you would write
print if $salary =~ /5/;or $salary =~ s/5/6/;
So, if you have a string that is not stored in the $_ variable and need to perform matches or substitutions on that string, the pattern binding operators =~ or !~ are used. They are also used with the tr function for string translations.
The pattern matching operators are listed in Table 8.3.
Table 8.3. Pattern Matching Operators
Example |
Meaning |
$name =~ /John/ |
True if $name contains pattern. Returns 1 for true, null for false. |
$name !~ /John/ |
True if $name does not contain pattern. |
$name =~ s/John/Sam/ |
Replace first occurrence of John with Sam. |
$name =~ s/John/Sam/g |
Replace all occurrences of John with Sam. |
$name =~ tr/a–z/A–Z/ |
Translate all lowercase letters to uppercase. |
$name =~ /$pal/ |
A variable can be used in the search string. |
Example 8.34
(The Script) # Using the $_ scalar explicitly 1 while($_=<DATA>){ 2 print $_ if $_ =~ /Igor/; # $_ holds the current input line 3 # print if /Igor/; } _ _DATA_ _ Steve Blenheim Betty Boop Igor Chevsky Norma Cord Jon DeLoach Karen Evich (Output) Igor Chevsky
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line following the _ _DATA_ _ token is read in and assigned to $_. Each time the loop is entered, the next line after _ _DATA_ _ is assigned to $_ until all the lines have been processed.
- If the regular expression /Igor/ is matched in the $_ variable, the print function will print the value of $_. The =~ is necessary here only if the $_ scalar is explicitly used as an operand.
- If the =~ pattern matching operator is omitted, the default is to match on $_, and if the print function is given no arguments, the value of $_ is also printed.
Example 8.35
(The Script) #!/usr/bin/perl 1 $name="Tommy Tuttle"; 2 print "Hello Tommy\n" if $name =~ /Tom/; # Prints Hello Tommy,if true 3 print "$name\n" if $name !~ /Tom/; # Prints nothing if false 4 $name =~ s/T/M/; # Substitute first T with an M 5 print "$name.\n"; 6 $name="Tommy Tuttle"; 7 print "$name\n" if $name =~ s/T/M/g; # Substitute every T with M 8 print "What is Tommy's last name? "; 9 print "You got it!\n" if <STDIN> =~ /Tuttle/; (Output) 2 Hello Tommy 5 Mommy Tuttle. 7 Mommy Muttle 8 What is Tommy's last name? Tuttle 9 You got it!
Explanation
- The scalar $name is assigned Tommy Tuttle.
- The string $name is printed if $name contains the pattern Tom. The return value from a successful match is 1.
- The string $name is not printed if $name does not contain the pattern Tom. The return value from an unsuccessful match is null.
- The first occurrence of the letter T in $name is replaced with the letter M.
- $name is printed, reflecting the substitution.
- $name is assigned Tommy Tuttle.
- All occurrences of the letter T in $name are replaced with the letter M. The g at the end of the substitution expression causes a global replacement across the line.
- User input is requested.
- The user input (<STDIN>) is matched against the regular expression Tuttle, and if there is a match, the print statement is executed.
Example 8.36
(The Script) 1 $salary=50000; 2 $salary =~ s/$salary/$& * 1.1/e; 3 print "\$& is $&\n"; 4 print "The salary is now \$$salary.\n"; (Output) 3 $& is 50000 4 The salary is now $55000.
Explanation
- The scalar $salary is assigned 50000.
- The substitution is performed on $salary. The replacement side evaluates the expression. The special variable $& holds the value found on the search side. To change the value in $salary after the substitution, the pattern matching operator =~ is used. This binds the result of the substitution to the scalar $salary.
- The $& scalar holds the value of what was found on the search side of the substitution.
- The scalar $salary has been increased by 10%.
Example 8.37
(The Script) # Using split and pattern matching 1 while(<DATA>){ 2 @line = split(":", $_); 3 print $line[0],"\n" if $line[1] =~ /408-/ # Using the pattern matching operator } 4 _ _DATA_ _ Steve Blenheim:415-444-6677:12 Main St. Betty Boop:303-223-1234:234 Ethan Ln. Igor Chevsky:408-567-4444:3456 Mary Way Norma Cord:555-234-5764:18880 Fiftieth St. Jon DeLoach:201-444-6556:54 Penny Ln. Karen Evich:306-333-7654:123 4th Ave. (Output) Igor Chevsky
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line following the _ _DATA_ _ token is read in and assigned to $_. Each time the loop is entered, the next line from _ _DATA_ _ is assigned to $_ until all the lines have been processed.
- Each line from the file will be split at the colons and the value returned stored in an array, @line.
- The pattern /408–/ is matched against the array element $line[1]. If that pattern is matched in $line[1], the value of $line[0] is printed. Prints Igor's name, $line[0], because his phone, $line[1], matches the 408 area code.
- The text following _ _DATA_ _ is used as input by the special DATA filehandle.
Example 8.38
(The Script) # Using split, an anonymous list, and pattern matching 1 while(<DATA>){ 2 ($name, $phone, $address) = split(":", $_); 3 print $name if $phone =~ /408-/ # Using the pattern # matching operator } 4 _ _DATA_ _ Steve Blenheim:415-444-6677:12 Main St. Betty Boop:303-223-1234:234 Ethan Ln. Igor Chevsky:408-567-4444:3456 Mary Way Norma Cord:555-234-5764:18880 Fiftieth St. Jon DeLoach:201-444-6556:54 Penny Ln. Karen Evich:306-333-7654:123 4th Ave. (Output) Igor Chevsky
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line after the _ _DATA_ _ token is read in and assigned to $_. Each time the loop is entered, the next line following _ _DATA_ _ is assigned to $_ until all the lines have been processed.
- Each line from the file will be split at the colons and the value returned stored in an anonymous list consisting of three scalars: $name, $phone, and $address. Using the anonymous list makes the program easier to read and manipulate than in the previous example where an array was used. With the array, you have to make sure you get the right index number to represent the various fields, whereas the named scalars are straightforward.
- The pattern /408-/ is matched against the $phone variable. If that pattern is matched in $phone, the value of $name is printed. Igor's name is printed because his phone matches the 408 area code.
- The text following _ _DATA_ _ is used as input by the special DATA filehandle.
Example 8.39
(The Script) 1 while($inputline=<DATA>){ 2 ($name, $phone, $address) = split(":", $inputline); 3 print $name if $phone =~ /^408-/; # Using the pattern # matching operator 4 print $inputline if $name =~ /^Karen/; 5 print if /^Norma/; } 6 _ _DATA_ _ Steve Blenheim:415-444-6677:12 Main St. Betty Boop:303-223-1234:234 Ethan Ln. Igor Chevsky:408-567-4444:3456 Mary Way Norma Cord:555-234-5764:18880 Fiftieth St. Jon DeLoach:201-444-6556:54 Penny Ln. Karen Evich:306-333-7654:123 4th Ave. (Output) 3 Igor Chevsky 4 Karen Evich:306-333-7654:123 4th Ave. 5 < No output >
Explanation
- The special DATA filehandle gets its input from the text after the _ _DATA_ _ token. The while loop is entered and the first line after the _ _DATA_ _ token is read in and assigned to a user-defined variable, $inputline, rather than $_. Each time the loop is entered, the next line from _ _DATA_ _ is assigned to $inputline until all the lines have been processed.
- Each line from the file, stored in $inputfile, will be split at the colons and the value returned stored in an anonymous list consisting of three scalars: $name, $phone, and $address.
- The pattern /408-/ is matched against the $phone variable. If that pattern is matched in $phone, the value of $name is printed. Prints Igor's name because his phone matches the 408 area code.
- Each line is stored in $inputline, one after the other, until the end of the file is reached. The value of $inputline is displayed if it begins with the regular expression Karen.
- Since the default line holder, $_, is no longer being used, nothing is assigned to it, and nothing is matched against it or displayed. The lines are now being stored and matched in the user-defined variable $inputline.
- The text following _ _DATA_ _ is used as input by the special DATA filehandle.