O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A.12. Chapter 13

  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 doesn't handle error reporting, or the -f or -i options, or anything like that, but that'd just be gravy. If you did that, 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 where 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. This means that renaming /usr/src/fred to /etc results in really renaming /usr/src/fred to /etc/fred.

    Finally, once the basename is patched up, we're home free, with a rename invocation.

  3. 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
    }
    link($old,$new) || die "Cannot link $old to $new: $!";

    This program is identical to the previous program ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required