Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HowTO -execute a command from perl and manipulte its output.

Status
Not open for further replies.

shlomyb

Programmer
Nov 14, 2006
32
IL
Hi

I want to execute a "cp" command from a perl script and in case the file does not exist I want to get and manipulate the output and do some more actions before I terminate the script.how?

Thanks.



 
Use File::Copy, it's much easier and platform independent.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
And in case I dont want / can not use a module , is there another way ?


Thanks for you quick reply.
 
AFAIK, File::Copy is part of the standard perl distribution, so you should already have it. If not you can shell to the OS using system, exec or backticks (qx), try the File::Copy first, as steve says it makes your code portable ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Try this:
my $cmd= "cp $from $to";
my $rc= 0xffff & system (" $cmd ");
unless ($rc==0){die "cp failed !";
#you can substitute the die by whatever error hander you want
}#unless rc =0

Actually this is a step more than what you want; it says
die (or whatever erro handler you want) if the cp fails.
The cp may fail for other reasons too, like not enough space

So if all you're worried about is whether the file exists
you can do
if(-e $from){my $rc=.... ; unless($rc==0){....}#proceed
} #of -e $from

This ought to solve your problem
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top