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

Re: Recommended file-copying method

Thread Previous | Thread Next
David Golden
July 8, 2013 17:38
Re: Recommended file-copying method
Message ID:
On Sun, Jul 7, 2013 at 11:36 PM, Craig A. Berry <> wrote:
> On Sun, Jul 7, 2013 at 8:29 PM, David Golden <> wrote:
>> * 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
> Anything that claims to be a file copy but is at all concerned about
> the encoding of the contents of the file is not actually a file copy.
> Trying to maintain otherwise is a major layer violation and is
> shooting yourself in the head with a bazooka.  And while there is
> probably no Perl primitive that is an especially good basis for an
> efficient file copy, sysread is likely the best one there is.

I think it depends if you do the open or just take a handle.  If you
do the open,  you can open with :unix only, bypass :perlio buffering
and use read() for the size of the file (for files with an available
size).  That's effectively sysread() down in PerlIO, but does all the
bytes-read accounting for you (and in C rather than Perl, to boot).
Ditto with print() on the output side.  Path::Tiny does that for raw
slurp/spew and it's very fast compared to other approaches.

The fact that File::Copy takes handles at all is an API design that I
take issue with for the reason you mention, among others.

Which reminds me of another nit:

* It doesn't flock handles for reading/writing.

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