Perl copy vs system cp | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Perl copy vs system cp

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-01-2010
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

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

2)

Code:
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.

Thanks.

Last edited by Scott; 01-01-2010 at 12:32 PM.. Reason: Please use code tags
Sponsored Links
    #2  
Old 01-01-2010
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
spaces

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
    #3  
Old 01-01-2010
pludi's Avatar
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,522
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.
    #4  
Old 01-01-2010
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 08:42 AM
Copy a file using UNIX-system calls c_d Programming 6 03-25-2009 12:00 PM
copy directory structure to a system on the network firefox211 Shell Programming and Scripting 1 10-14-2008 03:55 PM
System copy with flarcreate pasalagua Solaris 2 02-06-2008 09:30 PM
on solaris best utility to copy a file system? JAKEZ Filesystems, Disks and Memory 1 01-20-2005 11:55 AM



All times are GMT -4. The time now is 03:15 PM.