Home > Articles > Web Development > Perl

This chapter is from the book

This chapter is from the book

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

  1. The m operator is not needed in this example, since forward slashes delimit the regular expression.
  2. The forward slash is the delimiter; therefore, the m operator is optional.
  3. 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.
  4. 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.
  5. 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}.
  6. 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

  1. 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 $_.
  2. All lines that match the pattern Betty are matched and printed.
  3. 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

  1. 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 $_.
  2. All lines that don't match the pattern Evich are printed.
  3. 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

  1. 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 $_.
  2. 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.
  3. 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

  1. 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 $_.
  2. 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.
  3. The scalar $name is assigned Jon.
  4. $_ is assigned a string including the scalar $name.
  5. 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.
  6. If double quotes enclose the expression, the variable $name will be interpolated. The string assigned to $_ is printed if it contains Jon.
  7. 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

  1. The $_ scalar is assigned a string.
  2. 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, $&.
  3. The search pattern Francisco was successfully matched, saved in $&, and printed.
  4. 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).
  5. The value of $` is printed.
  6. 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).
  7. 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

  1. The $_ scalar is assigned a string.
  2. 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.
  3. 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

  1. 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 $_.
  2. 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.)
  3. Each line will be printed, whether or not the substitution occurred.
  4. 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

  1. 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 $_.
  2. 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.
  3. 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

  1. 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 $_.
  2. 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.
  3. 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

  1. 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 $_.
  2. The search pattern Blenheim is delimited with parentheses and the replacement pattern, Dobbins, is delimited with forward slashes.
  3. The substitution is shown in the output when it is printed. Blenheim is replaced with Dobbins.
  4. 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

  1. 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 $_.
  2. With the g option, the substitution is global. Every occurrence of Tom will be replaced with Christian for each line that is read.
  3. 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

  1. 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.
  2. 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.
  3. 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

  1. The $_ scalar is assigned 5.
  2. 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.
  3. The result of the evaluation is printed.
  4. The $_ variable is assigned 1055.
  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 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.
  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

  1. The $_ variable is the string knock at heaven's door.\n;
  2. 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.
  3. 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

  1. The $_ scalar is assigned 5000.
  2. 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.
  3. The resulting value is printed.
  4. The $_ scalar is assigned the string knock at heaven's door.\n.
  5. 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

  1. 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.
  2. 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.
  3. 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

  1. The scalar $name is assigned Tommy Tuttle.
  2. The string $name is printed if $name contains the pattern Tom. The return value from a successful match is 1.
  3. The string $name is not printed if $name does not contain the pattern Tom. The return value from an unsuccessful match is null.
  4. The first occurrence of the letter T in $name is replaced with the letter M.
  5. $name is printed, reflecting the substitution.
  6. $name is assigned Tommy Tuttle.
  7. 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.
  8. User input is requested.
  9. 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

  1. The scalar $salary is assigned 50000.
  2. 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.
  3. The $& scalar holds the value of what was found on the search side of the substitution.
  4. 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

  1. 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.
  2. Each line from the file will be split at the colons and the value returned stored in an array, @line.
  3. 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.
  4. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. The text following _ _DATA_ _ is used as input by the special DATA filehandle.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020