develooper Front page | perl.perl5.porters | Postings from July 2013

Re: Recommended file-copying method

Thread Previous | Thread Next
David Golden
July 8, 2013 01:29
Re: Recommended file-copying method
Message ID:
On Sun, Jul 7, 2013 at 6:38 PM, Ken Williams <> wrote:
> My question about the best choice for the underlying copy operation still
> stands, if someone knows of a good choice.

I've been doing some background work on this for implementing saner
move/copy/rename for Path::Tiny.

My conclusion is that File::Copy isn't great, but it's probably the
best thing available in core.  I'd probably recommend the 'cp'
function as I think it's going to be the most portable and least
surprising to the most people.

What's wrong with it?  It has a number of suboptimal API and
implementation choices:

* copies are not atomic -- interruptions or errors leave half-copied
files around

* copying a file on top of itself warns and returns 0 -- technically
correct, but not very DWIMy

* copying to/from handles requires caller to set raw binmode -- plenty
of opportunity to shoot oneself in the foot, even if its documented

* uses sysread instead of read, so if your handle wasn't raw for some
reason, layers are ignored (except for the utf8 flag, which winds up
lax instead of possibly strict) so you're shooting your foot with a
machine gun

* cp preserves only mode, not ownership (if root) or timestamps

On the positive, it supposedly works on VMS (which isn't a
consideration for Path::Tiny).

For what Path::Class needs to do, it can probably avoid or work around
those issues.

Path::Tiny is probably going to reimplement it in terms of raw
slurp/spew with optional chown/chmod/utime (which is tricky-ish but
doesn't seem too bad).


David Golden <>
Take back your inbox! →
Twitter/IRC: @xdg

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About