Answer to Chapter 12 Exercise

A.11 Answers to Chapter 12 Exercises

1.       Here's one way to do it, with a glob:

2.          print "Which directory? (Default is your home directory) ";
3.          chomp(my $dir = <STDIN>);
4.          if ($dir =~ /^\s*$/) {  # A blank line
5.           chdir or die "Can't chdir to your home directory: $!"; 
6.          } else {
7.           chdir $dir or die "Can't chdir to '$dir': $!";
8.          }
9.           
10.     my @files = <*>;
11.     foreach (@files) {
12.      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.

Finally, a glob on "star" pulls up all the names in the (new) working directory, automatically sorted to alphabetical order, and they're printed one at a time.

13.   Here's one way to do it:

14.     print "Which directory? (Default is your home directory) ";
15.     chomp(my $dir = <STDIN>);
16.     if ($dir =~ /^\s*$/) {  # A blank line
17.      chdir or die "Can't chdir to your home directory:
18.     $!"; 
19.     } else {
20.      chdir $dir or die "Can't chdir to '$dir': $!";
21.     }
22.      
23.     my @files = <.* *>;  ## now includes .*
24.     foreach (sort @files) {  ## now sorts
25.      print "$_\n";
}

Two differences from previous one: first, the glob now includes "dot star", which matches all the names that do begin with a dot. And second, we now must sort the resulting list, because some of the names that begin with a dot must be interleaved appropriately either before or after the list of things without a beginning dot.

26.   Here's one way to do it:

27.     print "Which directory? (Default is your home directory) ";
28.     chomp(my $dir = <STDIN>);
29.     if ($dir =~ /^\s*$/) {  # A blank line
30.      chdir or die "Can't chdir to your home directory:
31.     $!"; 
32.     } else {
33.      chdir $dir or die "Can't chdir to '$dir': $!";
34.     }
35.      
36.     opendir DOT, "." or die "Can't opendir dot: $!";
37.     foreach (sort readdir DOT) {
38.      # next if /^\./; ##  if we were skipping dot files
39.     print "$_\n";
}

Again, same structure as the previous two programs, but now we've chosen to open a directory handle. Once we've changed the working directory, we want to open the current directory, and we've shown that as the DOT directory handle.

Why DOT? Well, if the user asks for an absolute directory name, like /etc, there's no problem opening it. But if the name is relative, like fred, let's see what would happen. First, we chdir to fred, and then we want to use opendir to open it. But that would open fred in the new directory, not fred in the original directory. The only name we can be sure will mean "the current directory" is ".", which always has that meaning (on Unix and similar systems, at least).

The readdir function pulls up all the names of the directory, which are then sorted, and displayed. If we had done the first exercise this way, we would have skipped over the dot files, and that's handled by the uncommenting the commented-out line in the foreach loop.

You may find yourself asking, "Why did we chdir first? You can use readdir and friends on any directory, not merely on the current directory." Primarily, we wanted to give the user the convenience of being able to get to their home directory with a single keystroke. But this could be the start of a general file-management utility program; maybe the next step would be to ask the user which of the files in this directory should be moved to offline tape storage, say.

 



Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2001
Pages: 205

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net