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)?
new2ss
May 25, 2006, 10:01pm
3
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:
Behavior of this function varies wildly depending on your system implementation. For example, it will usually not work across file system boundaries, even though the system mv command sometimes compensates for this. Other restrictions include whether it works on directories, open files, or pre-existing files. Check perlport and either the rename(2) manpage or equivalent system documentation for details.
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.
new2ss:
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.
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:
This package is included as a show case, illustrating a few Perl features. It shouldn't be used for production programs. Although it does provide a simple interface for obtaining the standard output of arbitrary commands, there may be better ways of achieving what you need.
...
Since Shell.pm and all of the aforementioned techniques use your system's shell to call some local command, none of them is portable across different systems. Note, however, that there are several built in functions and library packages providing portable implementations of functions operating on files, such as: glob, link and unlink, mkdir and rmdir, rename, File::Compare, File::Copy, File::Find etc.
That's why the File::Copy module came to my mind instead of Shell.
new2ss
May 28, 2006, 10:59pm
10
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.