Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: A.11 Chapter 12, Directory AccessAppendix A
Exercise Answers
Next: A.13 Chapter 14, Process Management
 

A.12 Chapter 13, File and Directory Manipulation

  1. Here's one way to do it:

    unlink @ARGV;

    Yup, that's it. The @ARGV array is a list of names to be removed. The unlink operator takes a list of names, so we just marry the two, and we're done.

    Of course, this solution doesn't handle error reporting, or the -f or -i options, or anything like that, but those things are just gravy. If your solution addressed these things, good!

  2. Here's one way to do it:

    ($old, $new) = @ARGV; # name them
    if (-d $new) { # new name is a directory, need to patch it up
      ($basename = $old) =~ s#.*\\##s; # get basename of $old
      $new .= "\\$basename"; # and append it to new name
    }
    rename($old,$new) || die "Cannot rename $old to $new: $!";

    The workhorse in this program is the last line, but the remainder of the program is necessary for the case in which the name we are renaming to is a directory.

    First, we give understandable names to the two elements of @ARGV. Then, if the $new name is a directory, we need to patch it by adding the basename of the $old name to the end of the new name. Finally, after the basename is patched up, we're home free, with a rename invocation.


Previous: A.11 Chapter 12, Directory AccessLearning Perl on Win32 SystemsNext: A.13 Chapter 14, Process Management
A.11 Chapter 12, Directory AccessBook IndexA.13 Chapter 14, Process Management