perl rename failed

i have a file, which i want to rename, from my current directory to a backupdirectory. My perl code is below

rename($s_filename,$backup.$s_filename ) 

all my variables are defined. but the rename failed. the file permission my file is "-rw-rw-r--". could this be a problem?

I tried to use the mv command from the shell enviromnent, it works. could anyone advise the cause?

Please, you have a previous thread on this subject started here:

Do not start another one.

As I said in that post, moving a file involves directory permissions, not file permissions. Try instead

if (!rename($s_filename,$backup.$s_filename )) {
    print $!, "\n";
}

Is anything shown (in fact you should always trap failures like this when dealing with file operations)?

Hi cbkihong,
the trap message is "Invalid cross-device link"

Sounds like you are trying to move a file between filesystems.

As Perl's rename is tied to the rename(2) system call, please check out the manpage for your system to check if cross-device move is not allowed. If so, then you may need to copy the file to the destination, and then remove the original one.

Don't overlook the footnote at the rename() function from the Perl documentation:

Hi cbkihong,

i tried to use the following, and it was successful. Thank you.

mv($s_filename,$newfile);

As advised by you, i should include a mechanism to capture any trap, how can i do it? Is it

if ( mv($s_filename,$newfile) )
{print "error";}

i read somewhere that if a system command fails, it sets a high bit, so to test for failure, the above way is suggested.

I realise even if i do not include the mechanism, and it fails, the error message will be printed, so i still need to explicitly include the mechanism?

I believe its more of a program design issue, would appreciate your advise.

There is officially not a "mv" function in Perl. Do you know where yours come from?

Or did you use this?
http://perldoc.perl.org/File/Copy.html

I did not have much experience with this module.

For system calls, such as rename() as you used, check $! for error number (or string, depending on how you use) as in my earlier post. Please read

http://perldoc.perl.org/perlvar.html

for the manual. For File::Copy, you may also read that manpage for expected return value and error status reporting. It should be similar.

i included this line in the beginnining of my script.

use SHELL qw(mv);
mv ( $oldfile, $newfile);

I would like to remind you of a warning that has been in the documentation concerning the "Shell" module:

That's why the File::Copy module came to my mind instead of Shell.

Hi cbkihong,

thanks for the reminder. I overlooked the portability issue. i think i'll use the below code instead.

use File::Copy;
move("$old","$new") or print "$!";

Appreciated your help. thanks again.