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!
($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.