Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 743
James Shade Avatar asked Dec 29 '22 12:12

James Shade


2 Answers

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.

like image 77
brian d foy Avatar answered Jan 04 '23 16:01

brian d foy


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

like image 40
ghostdog74 Avatar answered Jan 04 '23 16:01

ghostdog74