Blob


1 #!/usr/bin/perl
3 # Write a program that asks the user to enter a pattern (regular
4 # expression). Read this as data from standard input; don't get it from the
5 # command line arguments. Report a list of files in some hardcoded
6 # directory (such as "/etc" or 'C:\\Windows') whose names match the
7 # pattern. Repeat this until the user enters an empty string instead of a
8 # pattern. The user should not type the slashes traditionally used to
9 # delimit pattern matches in Perl; the input pattern is delimited by the
10 # trailing newline. Ensure that a faulty pattern, such as one with
11 # unbalanced parentheses, doesn't crash the program.
13 use v5.24;
14 use warnings;
15 use strict;
16 use utf8;
18 chdir or die "Unable to cd to ~: '$!'";
19 my $pattern;
20 print "Regex pattern: ";
21 while (chomp($pattern = <>) && $pattern ne '') {
22 print "\n";
23 print join("\n", grep eval { /$pattern/ }, glob(".* *"));
24 print "\n\nRegex pattern: ";
25 }