home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


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.