Blob


1 #!/usr/bin/perl
3 # Write a program to ask the user for a directory name, then change
4 # to that directory. If the user enters a line with nothing but
5 # whitespace, change to his or her home directory as a default.
6 # After changing, list the ordinary directory contents (not the
7 # items whose names begin with a dot) in alphabetical order. (Hint:
8 # will that be easier to do with a directory handle or with a
9 # glob?) If the directory change doesn't succeed, just alert the
10 # user--but don't try showing the contents.
12 use v5.24;
13 use warnings;
14 use strict;
15 use utf8;
16 use Cwd;
18 if (scalar(@ARGV) > 1) {
19 die "Usage: $0 [dir]";
20 }
21 my $dir = shift @ARGV;
22 if (!defined($dir) || $dir =~ /\A\s*\z/) {
23 chdir or die "Unable to change to $ENV{HOME}: $!";
24 } else {
25 chdir $dir or die "Unable to change to $dir: $!";
26 }
28 my @files = glob '*';
29 print join "\n", @files;
30 print "\n";