Blob


1 #!/usr/bin/perl
3 # Write a program to read in a list of patterns from a file. Precompile the
4 # patterns and store them in an array. For example, your patterns file
5 # might look like:
6 #
7 # cocoa?n[ue]t
8 # Mary[-\s]+Anne?
9 # (The\s+)?(Skipper|Professor)
10 #
11 # Prompt the user for lines of input, printing the line number and text for
12 # each line that matches. The $. variable is useful here.
14 use v5.24;
15 use warnings;
16 use strict;
17 use utf8;
18 use local::lib;
19 use Getopt::Std;
21 my %opts;
22 getopt("p:", \%opts);
23 if (defined($opts{p}) && scalar(%opts) == 1) {
24 open my $fh, "<", $opts{p} or die "Unable to open '$opts{p}': $!";
25 my @patterns;
26 while (<$fh>) {
27 chomp;
28 push @patterns, eval { qr/$_/ };
29 }
30 print "Type some input:\n\n";
31 while (<>) {
32 print "$.: $_" if $_ ~~ @patterns;
33 }
34 } else {
35 die "Usage: $0 -p file";
36 }