Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Perl copy vs system cp

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 01-01-2010   -   Original Discussion by slchin
slchin slchin is offline
Registered User
Join Date: Dec 2009
Last Activity: 2 January 2010, 10:51 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Perl copy vs system cp

What are the pros & cons, if any, between using Perl's copy module vs OS's system cp, for copying a file to another directory? Or are they exactly the same?

1) Perl's File::Copy module, as in

copy ($filename, $dest_path) or die "ERROR: Cannot copy\n";


if (system ("cp $filename, $dest_path") != 0) {
  <do something>;
} else {
  print "ERROR: Cannot copy\n";

PS. Sorry if this is already discussed in other/older threads.


Last edited by Scott; 01-01-2010 at 01:32 PM.. Reason: Please use code tags
Sponsored Links
Old Unix and Linux 01-01-2010   -   Original Discussion by slchin
m1xram m1xram is offline
Registered User
Join Date: Nov 2009
Last Activity: 31 August 2011, 10:26 PM EDT
Posts: 61
Thanks: 1
Thanked 9 Times in 9 Posts

The one thing I can see from your example is that the system call won't work if the files have spaces in the names but File::Copy will. That could easily be fixed but it is something you have to take into account.

Staying within Perl will probably mean that another process is not forked. The 'system' call always forks another process. That can be an advantage or a disadvantage depending on the behaviour you desire. For instance, if you wanted to copy ten files but didn't wish to wait for completion, the 'cp's could be forked asynchronously.
Sponsored Links
Old Unix and Linux 01-01-2010   -   Original Discussion by slchin
pludi's Unix or Linux Image
pludi pludi is offline Forum Advisor  
Cat herder
Join Date: Dec 2008
Last Activity: 28 March 2014, 8:35 AM EDT
Location: Vienna, Austria, Earth
Posts: 5,521
Thanks: 38
Thanked 335 Times in 308 Posts
IMO the best argument for not using a system utility if there's an equivalent Perl-based solution: portability. File::Copy will work on any system that can run Perl. cp won't work on any platform where the file duplication utility isn't called that.
Old Unix and Linux 01-01-2010   -   Original Discussion by slchin
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
Join Date: Mar 2005
Last Activity: 10 January 2017, 8:10 AM EST
Location: classification algos
Posts: 3,215
Thanks: 19
Thanked 31 Times in 28 Posts
if (system ("cp $filename, $dest_path") != 0) {
<do something>;
} else {
print "ERROR: Cannot copy\n";
Related to this thread but slightly unrelated to this ...

when using system command ( if that is unavoidable in some scripts, I know there are equivalents for almost everything ), always use the absolute path of the command like /bin/cp and not just cp

cp could be simply aliased to something else and you are not executing 'cp' but something else.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to copy my system hdd usb stick from 4GB to 8GB ? jack2 UNIX for Dummies Questions & Answers 7 04-03-2009 09:42 AM
Copy a file using UNIX-system calls c_d Programming 6 03-25-2009 01:00 PM
copy directory structure to a system on the network firefox211 Shell Programming and Scripting 1 10-14-2008 04:55 PM
System copy with flarcreate pasalagua Solaris 2 02-06-2008 10:30 PM
on solaris best utility to copy a file system? JAKEZ Filesystems, Disks and Memory 1 01-20-2005 12:55 PM

All times are GMT -4. The time now is 01:26 PM.