Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
P is for Practical
 
PerlMonks  

File::Copy or system

by Anonymous Monk
on Sep 28, 2011 at 13:06 UTC ( #928316=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In a pure unix environment, is File::Copy::copy($a,$b) preferred to system('cp',$a,$b)?

Presumably, the system call has some process creation overhead, yet I think that system has higher readability.

I think in mixed environments, the case for File::Copy is obvious. There may also be a security reason to avoid calls to system in shared-user environments; also if search path management is poor (e.g., someone sets up a bad cron environment) then File::Copy can be necessitated. I am working in a secure environment where path management should be second nature.

Comment on File::Copy or system
Re: File::Copy or system
by CountZero (Chancellor) on Sep 28, 2011 at 13:38 UTC
    If you are fond of the cp command and still want to avail yourself of the nice extras provided by File::Copy, just do use File::Copy qw/cp/; at the start of your program and the cp function is imported into your namespace.

    With Perl, you can can have your cake and eat it!

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: File::Copy or system
by Riales (Hermit) on Sep 28, 2011 at 17:26 UTC

    I don't know anything about the security aspect of it, but I was curious about which one was faster, so I ran a benchmark...

    Copying about 1.5MB:

    Benchmark: running File::Copy::cp($a, $b), system('cp', $a, $b) for at + least 3 CPU seconds... File::Copy::cp($a, $b): 4 wallclock secs ( 0.17 usr + 3.18 sys = 3. +35 CPU) @ 339.70/s (n=1138) system('cp', $a, $b): 29 wallclock secs ( 0.13 usr 3.21 sys + 1.43 c +usr 24.26 csys = 29.03 CPU) @ 965.27/s (n=3224)

    I'd like to admit that I don't quite know how to parse the results. It looks like the system command incurs a 1.43 cusr and 24.26 csys penalty--what is that?

      File::Copy will be a native port and will expressed in terms of 'C' calls to the underlying O/S functions. 'C' functions will be called via .DLL's or .so's to do the job without needing a separate processes or programs.

      The end result is that Perl can copy or move a file faster than the command line shell (like bash, etc) can do it because there is no bash interpreter! (a separate program that has to get started and there are inefficiencies with that...).

      Your result is not surprising and is expected.

      A simple Perl program can beat the heck out of MS XCOPY.

      Well written Perl runs very quickly.

Re: File::Copy or system
by Marshall (Prior) on Sep 28, 2011 at 17:32 UTC
    File::Copy is a core module. 'Core' means that it is always there. And would be preferred to a system() call even in a pure Unix environment, meaning multi-platform code doesn't matter.

    yet I think that system has higher readability. I doubt that very much. copy or cp is a very, well used and debugged function and is fast.

Re: File::Copy or system
by pvaldes (Hermit) on Sep 28, 2011 at 22:23 UTC
    In a pure unix environment, is File::Copy::copy($a,$b) preferred to system('cp',$a,$b)?

    If what you want is to copy several files to a directory probably not, i.e:

    use File::Basename; use File::Copy; use Carp; open my $a, 'myfile.txt'; my ($f,$b,$c) = $fileparse("/home/me/my_dir/my_other_file.txt"); copy($a,$b) or croak; # Oops!
Re: File::Copy or system
by repellent (Priest) on Sep 30, 2011 at 06:30 UTC
    Consider what system(qw(/bin/cp -rp), $a, $b) can provide over File::Copy::copy($a, $b). Personally, I prefer the system approach.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://928316]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-04-20 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls