Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to copy one file to another. If I don't want to use system command such as 'system(cp ..)' or 'sytem(copy ..), is any other method to do this job easily ?
  • Comment on What is the easiest way to copy one file to another?

Replies are listed 'Best First'.
Re: What is the easiest way to copy one file to another?
by ar0n (Priest) on Oct 31, 2001 at 00:55 UTC
    Use File::Copy, it's part of the standard library:
    use File::Copy; copy("/path/to/old", "/path/to/new");

    [ ar0n ]

Re: What is the easiest way to copy one file to another?
by broquaint (Abbot) on Oct 31, 2001 at 02:28 UTC
    Well my first thought (well second after File::Copy :o) was to was just to read'n'write -
    open(ONE, "firstfile") or die("$!\n"); open(TWO, ">secondfile") or die("$!\n"); print TWO while <ONE>; close(ONE); close(TWO);
    That should suffice for text files, and conceivably binary files with binmode() on.
    HTH

    broquaint

    aside: Why is that close() only takes one filehandle? It would be infinitely more handy to have take multiple filehandles. Hmmm ...

    Update: once again my replying skills come in too slow ;o)

Re: What is the easiest way to copy one file to another?
by Masem (Monsignor) on Oct 31, 2001 at 01:09 UTC
    Another possibility:
    rename ( $oldfile, "$oldfile.bak" ) or die $!; rename ( $newfile, $oldfile ) or die $!;

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      ? This implements a file move, not a copy.

      I think the questioner wanted a piece of code that would produce two files with identical contents. This expects both $newfile and $oldfile to exist beforehand. It's very different from what File::Copy would do. (It's not wrong code, it just does something different.)

Re: What is the easiest way to copy one file to another?
by jwest (Friar) on Oct 31, 2001 at 01:12 UTC
    For a more fun, but less portable and unrecommended solution...

    link($oldfile, $newfile); unlink($oldfile);

    But you should really be using File::Copy as ar0n suggests.

    Update: Which, of course, actually performs a rename and not a copy. So, it's doubly true that File::Copy should be used instead.

    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
Re: What is the easiest way to copy one file to another?
by Jonathan (Curate) on Oct 31, 2001 at 22:39 UTC
    I'd use File::Copy but Larry's comments in the Shell module always make me smile so I'll suggest that.
    use Shell qw(cp); cp("fileName1","fileName2");
Re: What is the easiest way to copy one file to another?
by dug (Chaplain) on Oct 31, 2001 at 01:27 UTC
    Or, if you have RAM to spare or aren't working with large files, you could
    open (FH, "file.txt") or die "$!"; undef $/; $slurped_file = <FH>; # whole file resides in $slurped file now. open (CFH, ">copied_file.txt") or die "$!"; print CFH $slurped_file; # file.txt contents now reside in copied_file.txt. # close your filehandles.
    Probably not as efficient as a system call.
      Ugh. Use File::Copy. Or, at least, skip the slurp. :)
      open(FROM, 'file.txt') or die "Can't open file.txt: $!"; open(TO, '>copied_file.txt') or die "Can't open copied_file.txt: $!"; print TO while <FROM>;