Discussion Locked

perl script problem

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello all,
I have a simple perl statement that I can't get to work...

system("cp ") || print "copy failed\n" ;

The problem is that it copies the file and also prints the message. The return code $! is "No such file or directory". But the file copied to the right place. I have tried various sources and targets. I have tried with or without the trailing slash on the target directory and even hard coding the target file. I have tried or instead of ||.

Any help would be appreciated very much.

4 replies

by corey1981

Why don't you try using the perl File::Copy module.

#!/usr/bin/perl
use File::Copy;

copy('', '') or die "File cannot be copied.";

I have always had good results using this perl module.

by cpfeiffe

Sorry, meant to say I tried that. It didn't help either. My perl book says that all that really does is make the script portable from one OS to another. It still makes a system call in the end. Basically it figures out if you need to do cp for UX or copy for Windows, etc.

Anyway, I did find a viable solution. Using stat on the target file and then testing the modify time with M_. If it is < 0 it was copied, otherwise it wasn't.

Another work around is to use 'unless -e $targetfile' after the copy command.

This discussion is closed to new replies.