Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl move throws error, but does move the file

Tags:

perl

move

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!

like image 268
user2968944 Avatar asked Jan 11 '23 23:01

user2968944


1 Answers

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.

like image 182
TLP Avatar answered Jan 25 '23 08:01

TLP