On Mon, Aug 27, 2007 at 08:26:33PM -0500, John E. Malmberg wrote:
> John E. Malmberg wrote:
> >So should C<rename()> always follow UNIX protection conventions, or
> >should that be another feature?
> >
> >Should the same thing be done for C<unlink()>
On these I don't know.
> Another interesting wrinkle on UNIX is where foo1 and foo2 are both
> directories:
>
> rename('foo1', 'foo2') replaces foo2 with foo1.
>
> rename('foo1', 'foo2/') also replaces foo2 with foo1.
>
> This is counter to the shell mv command which in the second syntax for
> the filename results in the foo1 directory tree being placed under foo2.
>
> VMS users would also expect that behavior, especially with VMS syntax.
On Unix, the shell mv command differs from rename. rename ignore the trailing
'/' and always deletes the empty target directory:
$ perl -we 'rename "foo1", "foo2" or die $!'
$ ls -al foo1 foo2
ls: foo1: No such file or directory
foo2:
total 24
drwxr-xr-x 2 nick nick 512 Sep 15 10:29 .
drwxr-xr-x 37 nick nick 21504 Sep 15 10:29 ..
$ mkdir foo1
$ perl -we 'rename "foo1", "foo2/" or die $!'
$ ls -al foo1 foo2
ls: foo1: No such file or directory
foo2:
total 24
drwxr-xr-x 2 nick nick 512 Sep 15 10:28 .
drwxr-xr-x 37 nick nick 21504 Sep 15 10:29 ..
$ mkdir foo1
$ perl -we 'rename "foo1/", "foo2/" or die $!'
$ ls -al foo1 foo2
ls: foo1: No such file or directory
foo2:
total 24
drwxr-xr-x 2 nick nick 512 Sep 15 10:29 .
drwxr-xr-x 37 nick nick 21504 Sep 15 10:29 ..
$ mkdir foo1
$ perl -we 'rename "foo1/", "foo2" or die $!'
$ ls -al foo1 foo2
ls: foo1: No such file or directory
foo2:
total 24
drwxr-xr-x 2 nick nick 512 Sep 15 10:29 .
drwxr-xr-x 37 nick nick 21504 Sep 15 10:29 ..
mv ignores the trailing '/' and always moves 1 directory into the other:
$ mkdir foo1
$ mv foo1 foo2
$ ls -al foo1 foo2
ls: foo1: No such file or directory
foo2:
total 26
drwxr-xr-x 3 nick nick 512 Sep 15 10:31 .
drwxr-xr-x 37 nick nick 21504 Sep 15 10:31 ..
drwxr-xr-x 2 nick nick 512 Sep 15 10:31 foo1
$ mv foo2/foo1 foo1
$ mv foo1 foo2/
$ ls -al foo1 foo2
ls: foo1: No such file or directory
foo2:
total 26
drwxr-xr-x 3 nick nick 512 Sep 15 10:31 .
drwxr-xr-x 37 nick nick 21504 Sep 15 10:31 ..
drwxr-xr-x 2 nick nick 512 Sep 15 10:31 foo1
$ mv foo2/foo1 foo1
$ mv foo1/ foo2
$ ls -al foo1 foo2
ls: foo1: No such file or directory
foo2:
total 26
drwxr-xr-x 3 nick nick 512 Sep 15 10:31 .
drwxr-xr-x 37 nick nick 21504 Sep 15 10:31 ..
drwxr-xr-x 2 nick nick 512 Sep 15 10:31 foo1
$ mv foo2/foo1 foo1
$ mv foo1/ foo2/
$ ls -al foo1 foo2
ls: foo1: No such file or directory
foo2:
total 26
drwxr-xr-x 3 nick nick 512 Sep 15 10:31 .
drwxr-xr-x 37 nick nick 21504 Sep 15 10:32 ..
drwxr-xr-x 2 nick nick 512 Sep 15 10:31 foo1
[at least on FreeBSD]
both mv and rename() here are behaving according to the Open Group's spec:
http://www.opengroup.org/onlinepubs/009695399/utilities/mv.html
http://www.opengroup.org/onlinepubs/009695399/functions/rename.html
But perlfunc says VMS can do whatever it feels is right:
Behavior of this function varies wildly depending on your system
implementation. For example, it will usually not work across file system
boundaries, even though the system I<mv> command sometimes compensates
for this. Other restrictions include whether it works on directories,
open files, or pre-existing files. Check L<perlport> and either the
rename(2) manpage or equivalent system documentation for details.
So I don't know. I don't use VMS, so I don't know what seems sensible.
Nicholas Clark