Click here to get back home

FAQ 5.5 How can I copy a file?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 5.5 How can I copy a file? PerlFAQ Server 06-07-2008
Posted by PerlFAQ Server on June 7, 2008, 3:03 pm
Please log in for more thread options
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

5.5: How can I copy a file?


(contributed by brian d foy)

Use the File::Copy module. It comes with Perl and can do a true copy
across file systems, and it does its magic in a portable fashion.

use File::Copy;

copy( $original, $new_copy ) or die "Copy failed: $!";

If you can't use File::Copy, you'll have to do the work yourself: open
the original file, open the destination file, then print to the
destination file as you read the original.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Posted by Bill H on June 8, 2008, 7:01 am
Please log in for more thread options
> This is an excerpt from the latest version perlfaq5.pod, which
> comes with the standard Perl distribution. These postings aim to
> reduce the number of repeated questions as well as allow the community
> to review and update the answers. The latest version of the complete
> perlfaq is athttp://faq.perl.org.
>
> --------------------------------------------------------------------
>
> 5.5: How can I copy a file?
>
> =A0 =A0 (contributed by brian d foy)
>
> =A0 =A0 Use the File::Copy module. It comes with Perl and can do a true co=
py
> =A0 =A0 across file systems, and it does its magic in a portable fashion.
>
> =A0 =A0 =A0 =A0 =A0 =A0 use File::Copy;
>
> =A0 =A0 =A0 =A0 =A0 =A0 copy( $original, $new_copy ) or die "Copy failed: =
$!";
>
> =A0 =A0 If you can't use File::Copy, you'll have to do the work yourself: =
open
> =A0 =A0 the original file, open the destination file, then print to the
> =A0 =A0 destination file as you read the original.

What is the advantage of using this over using system("cp $original
$new_copy"); (or system("copy $original $new_copy"); on windows)? Is
it faster or is it just "perlish"? Or am I missing something obvious?

Bill H

PS When did Google Groups drop the captcha?

Posted by Jürgen Exner on June 8, 2008, 9:28 am
Please log in for more thread options
>>     Use the File::Copy module. It comes with Perl and can do a true copy
>
>What is the advantage of using this over using system("cp $original
>$new_copy"); (or system("copy $original $new_copy"); on windows)? Is
>it faster or is it just "perlish"? Or am I missing something obvious?

system() isn't portable.
File::Copy also avoids forking external processes, although the time
savings are probably pretty irrelevant given the nature of the
operation.

jue

Posted by Peter J. Holzer on June 8, 2008, 10:15 am
Please log in for more thread options
>> 5.5: How can I copy a file?
>>
>>     (contributed by brian d foy)
>>
>>     Use the File::Copy module. It comes with Perl and can do a true copy
>>     across file systems, and it does its magic in a portable fashion.
>>
>>             use File::Copy;
>>
>>             copy( $original, $new_copy ) or die "Copy failed: $!";
>>
>>     If you can't use File::Copy, you'll have to do the work yourself: open
>>     the original file, open the destination file, then print to the
>>     destination file as you read the original.
>
> What is the advantage of using this over using system("cp $original
> $new_copy"); (or system("copy $original $new_copy"); on windows)?

I think you just answered your question: The advantage is that you can
just write

copy( $original, $new_copy )

instead of

if (os_is_some_kind_of_unix()) {
        system("cp", $original, $new_copy);
} elsif (os_is_windows()) {
        system("copy", $original, $new_copy);
} else {
        ...
}

and wonder how you can write sub os_is_some_kind_of_unix.

(and you won't make stupid mistakes like system("cp $original
$new_copy"). which won't work if either $original or $new_copy contains
any of a large number of "dangerous" characters)

OTOH, when you use a system-specific tool like "cp", you can do
system-specific things, like preserve permissions or timestamps.


> Is it faster

It is probably faster for small files. It also is more robust (even if
the system has a "cp" command, it may not be in your path).

> or is it just "perlish"?

That, too.

        hp


Similar ThreadsPosted
Use of CGI.pm filehandle with File::Copy::copy in taint mode May 5, 2006, 3:57 pm
"copy" from File::Copy fails but returns success October 16, 2007, 4:43 am
File::Copy giving "Bad File Descriptor" from sysread October 3, 2004, 11:33 am
File::Copy works, File::NCopy doesn't September 13, 2005, 1:56 pm
file copy November 19, 2004, 2:45 pm
FAQ 5.5 How can I copy a file? May 2, 2005, 5:03 pm
FAQ 5.5 How can I copy a file? July 18, 2005, 4:03 pm
FAQ 5.5 How can I copy a file? September 22, 2005, 4:03 pm
FAQ 5.5 How can I copy a file? December 11, 2005, 11:03 pm
FAQ 5.5 How can I copy a file? January 21, 2006, 6:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap