Blame


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