Re: Perl Rename function unreliable?



Rhugga wrote:
> Has anyone experienced problems using perl rename() function? We have
> seen odd results using 3 different versions of perl.

"odd results" is not a particularly helpful error description.

> I have some code
> that renames some oracle archive logs and out of a run of say 100 logs
> it will fail to rename a handfull.

"fail"s in what way? The rename() function returns a false value, or
the rename() function returns true but the files remain un-renamed?

> I examine that file stats and they
> are all identical in every possible way. (just different ctime/atime
> etc...) Perms are identical. The files are not open by another process
> (even though this doesnt matter per the documentation)
>
> I even added a sleep(5) between each rename() call.
>
> My OS types are a mix of Solaris 8 and 9.

Have you read the caveats about this function in
perldoc -f rename
?

> I'm using the rename command as follows:
>
> rename( $oldfile, $newfile) || die "Unable to rename $oldfile, manual
> intervention required.";

You ask the progrma to die, but don't bother asking Perl to tell you
*why* rename() failed? Why are you not including the $! variable?

> Is using this any less robust:
>
> `/usr/bin/mv $oldfile $newfile`;

The above is "wrong" in several ways.
* It's not portable
* You're not checking the return value, so you have no idea if it
succeeded or not.
* back-ticks are not what you should be using unless you're trying to
capture the output... and you're not capturing the output. Please see:

perldoc -f system

Have you seen the posting guidelines for this group? Please post a
short-but-complete script that demonstrates your error.

Paul Lalli

.



Relevant Pages

  • rename and case insensitive filing systems
    ... It used the standard ... rename (oldfile, newfile); approach and I discovered, to my horror, some ... perl sucessfully deleted all the ... (Currently I am manually doing rename old TMP; rename TMP new which is just ...
    (comp.lang.perl.misc)
  • Re: getting rid of filenames with whitespaces
    ... > i'm using a Linux system running Samba as a filestore for a Windows client. ... > problem, but since some Unix commands cannot handle such files properly, ... Use the rename command, it's a perl script, that lets you use perl RE, ...
    (comp.os.linux.misc)
  • Re: file handle in perl
    ... I hear that Perl can do some of this stuff too. ... happens to file1 until that's finished. ... we rename file1 to file1.bak ... to do it yourself for more complex cases where a one-liner wouldn't cut it. ...
    (perl.beginners)
  • =?UTF-8?Q?Re:_The_rename_command=E2=80=A6?=
    ... For an individual call it is slower, but when renaming many files with ... one pattern I guess rename would be faster (mv can only rename one ... the source is the command itself. ... Perl is pretty fast at what it does, and perl is written in C. ...
    (Ubuntu)
  • =?UTF-8?Q?Re:_The_rename_command=E2=80=A6?=
    ... For an individual call it is slower, but when renaming many files with ... one pattern I guess rename would be faster (mv can only rename one ... the source is the command itself. ... Perl is pretty fast at what it does, and perl is written in C. ...
    (Ubuntu)