Search code examples
perlmove

perl move throws error, but does move the file


I'm pretty new to perl and just found some very unexpected behaviour.

I use move from File::Copy to rename a folder. It works as expected, but when I look at the return value variable $! afterwards, it shows an error.

The relevant code I use:

$helpr =~ s/\./ /g;

move($file,$helpr);
print $!;

The output:

[j@box test]$ ls
my.test.dir
[j@box test]$ fileRenamer.pl
No such file or directory
[j@box test]$ ls
my test dir

Why do I get an error code, when the job is done anyhow? What am I missing?

Thanks everyone!


Solution

  • As mpapec says, you should not use the error message unless move returns a false value, e.g. with ... or die $!. However, as to why this occurs:

    I've seen this before, and it seems like File::Copy is setting $! spuriously. I looked at the source and found this line:

    ($tosz1,$tomt1) = (stat($to))[7,9];
    

    Where $to is the file name that the file is being moved to. This check is made to handle exceptions made for overwriting files, and naturally if the file does not exist, $! will be set. I would classify this as a bug of sorts.