Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Here’s one way to do it, with a glob:
print "Which directory? (Default is your home directory) ";
chomp(my $dir = <STDIN>);
if ($dir =~ /\A\s*\Z/) { # A blank line
chdir or die "Can't chdir to your home directory: $!";
} else {
chdir $dir or die "Can't chdir to '$dir': $!";
}
my @files = <*>;
foreach (@files) {
print "$_\n";
}
First, we show a simple prompt, and read the desired
directory, chomping it as needed.
(Without a chomp, we’d be trying
to head for a directory that ends in a newline—legal in Unix, and
therefore cannot be presumed to simply be extraneous by the chdir function.)
Then, if the directory name is nonempty, we’ll change to that directory, aborting on a failure. If empty, the home directory is selected instead.