Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Perl's File::Copy appear to silently fail?

I have a Perl script that works on Windows XP. It uses File::Copy's move function to move a directory tree to another place on the same drive. The script fails (silently) on Windows 2008. Nothing moved, nothing deleted.

I am using ActiveState Perl 5.10.0 Build 1005 and the File::Copy that comes with it.

Anyone know of issues with ActiveState Perl on Windows 2008 that might cause this?

Example script:

use File::Copy;
print "Move AAA to ZZZ\n";

move("AAA", "ZZZ");

print "Done.\n";
like image 906
Paul Chernoch Avatar asked Nov 28 '22 12:11

Paul Chernoch


2 Answers

From the documentation:

RETURN

All functions return 1 on success, 0 on failure. $! will be set if an error was encountered.

The example fails silently because nothing is checking what $! is on failure. Try this:

move($from, $to) || die "Failed to move files: $!";
like image 186
caskey Avatar answered Dec 01 '22 02:12

caskey


If you don't want to have to check the return value, you can have autodie do it for you.

Since move() and copy() return a zero to indicate an error, and that's what autodie assumes, it's fairly straight forward.

use strict;
use warnings;
use File::Copy;

use autodie qw'copy move';

move("AAA", "ZZZ"); # no need to check for error, because of autodie

print "Done.\n";

Assuming that "AAA" doesn't exist, here is the output ( on STDERR ).

Can't move('AAA', 'ZZZ'): No such file or directory at test.pl line 7
like image 21
Brad Gilbert Avatar answered Dec 01 '22 03:12

Brad Gilbert