如何在 Perl 中设置文件最后修改的文件属性
How to set file last modified file attribute in Perl
在这里,我使用存储在本地目录中的 last_run
文件,如果它不等于当前日期时间,则更新它的最后修改日期。是否可以不在文件中写入符号,而是通过其他方式更新mtime?
我看到有一个模块File::Touch,但我不想使用它,因为我已经有一个打开的文件,并且按原样使用文件描述符会更快。
#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use File::stat;
use DateTime;
my $file_name = "last_run";
if (-e $file_name)
{
my $fh;
if (open($fh, "+<", $file_name))
{
my $timestamp = stat($fh)->mtime;
my $now = DateTime->now(time_zone => 'local');
my ($sec, $min, $hour, $day, $month, $year) = localtime($timestamp);
$month += 1;
$year += 1900;
if ($now->day != $day or $now->month != $month or $now->year != $year)
{
print $fh ' '; # <- I have to write something into the file
}
print "$day $month $year\n";
print $now->day.' '.$now->month.' '.$now->year."\n";
}
else
{
print "cannot open +< $file_name: $!";
}
close($fh);
}
else
{
open(my $fh, ">", $file_name)
or print "cannot open > file name: $!";
}
您正在查找 utime
函数。
文档中的一些引用:
Changes the access and modification times on each file of a list of files. The first two elements of the list must be the NUMERIC access and modification times, in that order. Returns the number of files successfully changed. The inode change time of each file is set to the current time.
Since Perl 5.8.0, if the first two elements of the list are undef, the utime(2)
syscall from your C library is called with a null second argument. On most systems, this will set the file's access and modification times to the current time.
On systems that support futimes(2)
, you may pass filehandles among the files. On systems that don't support futimes(2)
, passing filehandles raises an exception. Filehandles must be passed as globs or glob references to be recognized; barewords are considered filenames.
所以,类似于:
utime undef, undef, $fh;
会将文件的修改(和访问)时间更新为当前时间。如果您没有使用 大多数系统,文档中还有另一个示例明确使用当前时间。
futimes(2)
出现在 Linux 和 BSD 上。如果您使用不同的 OS,则必须使用文件名而不是句柄。
在这里,我使用存储在本地目录中的 last_run
文件,如果它不等于当前日期时间,则更新它的最后修改日期。是否可以不在文件中写入符号,而是通过其他方式更新mtime?
我看到有一个模块File::Touch,但我不想使用它,因为我已经有一个打开的文件,并且按原样使用文件描述符会更快。
#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use File::stat;
use DateTime;
my $file_name = "last_run";
if (-e $file_name)
{
my $fh;
if (open($fh, "+<", $file_name))
{
my $timestamp = stat($fh)->mtime;
my $now = DateTime->now(time_zone => 'local');
my ($sec, $min, $hour, $day, $month, $year) = localtime($timestamp);
$month += 1;
$year += 1900;
if ($now->day != $day or $now->month != $month or $now->year != $year)
{
print $fh ' '; # <- I have to write something into the file
}
print "$day $month $year\n";
print $now->day.' '.$now->month.' '.$now->year."\n";
}
else
{
print "cannot open +< $file_name: $!";
}
close($fh);
}
else
{
open(my $fh, ">", $file_name)
or print "cannot open > file name: $!";
}
您正在查找 utime
函数。
文档中的一些引用:
Changes the access and modification times on each file of a list of files. The first two elements of the list must be the NUMERIC access and modification times, in that order. Returns the number of files successfully changed. The inode change time of each file is set to the current time.
Since Perl 5.8.0, if the first two elements of the list are undef, the
utime(2)
syscall from your C library is called with a null second argument. On most systems, this will set the file's access and modification times to the current time.
On systems that support
futimes(2)
, you may pass filehandles among the files. On systems that don't supportfutimes(2)
, passing filehandles raises an exception. Filehandles must be passed as globs or glob references to be recognized; barewords are considered filenames.
所以,类似于:
utime undef, undef, $fh;
会将文件的修改(和访问)时间更新为当前时间。如果您没有使用 大多数系统,文档中还有另一个示例明确使用当前时间。
futimes(2)
出现在 Linux 和 BSD 上。如果您使用不同的 OS,则必须使用文件名而不是句柄。