views:

798

answers:

2

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"?

+2  A: 

Check File::Copy. Look under syscopy or rmscopy. It talks about timestamps there.

ghostdog74
http://perldoc.perl.org/File/Copy.html
emddudley
rmscopy seemed promising, but is only available on VMS systems (not Linux, which I am using). The formatting of the documentation makes this a bit unclear, but I have checked in the Copy.pm file in my 5.10.0 installation, and rmscopy isn't defined.
James Shade
+3  A: 

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.

brian d foy
stat and utime are an option, but don't really meed the "straightforward" criteria. Perhaps I am expecting too much, although I would find it surprising if the vast Perl libraries don't have a "copy and preserve attributes" function (that works on Linux).
James Shade
Maybe your definition of straightforward is different. As for being surprised about the lack of a good module, it's because no one cares to believe how broken File::Copy is, so no one comes up with a better one.
brian d foy
It looks like this is the best solution. In my program I've gone for `cp -p`, which is acceptable and simple for a fairly throwaway script that I don't intend to distribute, but I think the clean solution would be to wrap up the copy, stat and utime into a neat library function.
James Shade