Here’s one way to do it:
unlink @ARGV;
Yup, that’s it. The
@ARGV
array is a list of names to be removed. Theunlink
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!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 arename
invocation.
Get Learning Perl on Win32 Systems now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.