[tpm] Renaming files

Drew Sullivan drew at ss.org
Wed Sep 16 20:00:55 PDT 2009


On Wed, 2009-09-16 at 19:16 -0400, Tom Legrady wrote:

>         
>            s/"???_???.jpg"/"$date_???_???.jpg"/;     ##### <===
>            rename $oldname, $_  or
>                $_ = $oldname,
>                warn $_, ' not renamed: ', $!;
>         }


NEVER EVER trust a substitute works on unknown data.
if the substitute fails then you are trying to rename the file to
itself.  At best you get a needles warning, at worst the file goes away
since:

perldoc -f rename says:

Changes the name of a file; an existing file NEWNAME will be
clobbered.  Returns true for success, false otherwise.

This can be implemented by a "DOS delete" followed by a "DOS rename"
which will fail if the files exists so the delete must be executed
first. 

So since newname exists, it is first deleted then the rename fails with
a no such file error :-(





More information about the toronto-pm mailing list