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

I'm trying to copy a file from folder A to folder B and under the environment I'm in, I'd rather not use modules.

Is there an easy way of this?

Also, is it essentially the same thing as..

open(LOG, "file.ext") or die "$! oops"); my $data = <LOG>; close(LOG) open(LOG, ">/path/to/new/file.ext") or die ("$! oops"); print LOG $data; close(LOG);

Replies are listed 'Best First'.
Re: How to copy a file
by Corion (Patriarch) on Feb 27, 2008 at 19:37 UTC

    How about File::Copy? It comes with your Perl unless it's a fairly old Perl.

Re: How to copy a file
by ww (Archbishop) on Feb 27, 2008 at 20:22 UTC

    Preferred solution: Heed Corion's note above.

    FTR, while your code is "close," it won't do quite what you want unless your file.ext is a single, monolithic line. If, as is likely, your file.ext contains newlines, your copy will contain only the last line of the original.

    One (TIMTOWTDI) cure for that if your log is plain text):

    my $data; # ugly global; don't do this open(LOG, "file.ext") or die ("oops, could not open source", $!); local $/; # slurp mode (see perlvar Record Separator) $data2 = <LOG>; close(LOG); open(LOG2, ">","path/to/copy_of_file.ext") or die ("oops - could not o +pen target", $!); print LOG2 $data2; close(LOG2);

    For non-text files, see binmode; note also the modest changes in the die messages, which IMO (YMMV) are good practice, just to make sure that even if I glance only carelessly at the message, I'm still apt to note which file failed to open. Also note that you could do this by calling the shell's cp or copy method.

Re: How to copy a file
by Bloodrage (Monk) on Feb 27, 2008 at 21:04 UTC
    Perl can handle multiple filehandles at once...
    open OLD "<", $oldfile or die "Can't open $oldfile ($!)\n"; open NEW ">", $newfile or die "Can't open $newfile ($!)\n"; print NEW while(<OLD>); close OLD; close NEW;
    err... didn't actually test that... but I'm sure it works :)
      File::Copy will at least copy more than a line at a time...by default 2Mb at a time for files, 1k for non-files. Which is likely preferable to one line at a time. But I think I'd go with system("cp ...")

      I have used this approach numerous times (though after I discovered File::Copy I've used it much more). The only thing I missed (typical of my not seeing all the power one can get into a single line) is the 'print NEW while(<OLD>)' single line.

      I am curious that in the original Anonymous Monk's code the input from OLD is to a scalar, doesn't that just read in a single record from the OLD document?

      ack Albuquerque, NM
Re: How to copy a file
by jrsimmon (Hermit) on Feb 27, 2008 at 20:18 UTC
    To answer your first question, "Yes". You can do one of two things without including any modules whatsoever: The first is to use the system or backtick operators to access the native copy command on whatever system you are using. Depending on the system, it will be something like:
    $rc = `cp /path/to/old/file/file.xt /path/to/new/file/file.txt`;

    The second process is as you described. Read the original file, then write the new copy yourself. It won't be as fast as using a native copy, but it will do the job.