Search code examples
perlfilecopytimestamp

How can I copy a file in Perl and maintain its timestamp?


I am using the File::Copy module to copy a file in Perl:

#! /usr/bin/perl -w

use File::Copy;

copy("somefile.log", "copiedfile.log");

I would like to preserve timestamps (particularly the modification time), but this doesn't appear to be an option.

Is there a straightforward way to achieve this without resorting to making a system call to "cp -p"?


Solution

  • You can use stat to get the timestamp and utime to set it. If you can get away with it, system isn't always bad. You're never going to make something faster than cp -p. You'd think that File::Copy would do it, but on unix it doesn't. Abigail filed a RT #96158, but it was closed with no action. He has a presentation about its brokenness, but I haven't seen it online.