简体   繁体   English

Perl将上次运行时间写入文件

[英]Perl write last run time to file

(New to perl) (Perl的新手)

I'm trying to update an existing perl script to store in a file the last time that it was run, in order to do something "since" that date on the next run. 我正在尝试更新现有的perl脚本,以在上一次运行该文件时将其存储在文件中,以便在下次运行时“自”该日期起执行某项操作。

Is there a safe way to store this in a file that I can then read into a timestamp again? 有没有一种安全的方法可以将其存储在文件中,然后可以再次将其读入时间戳?

I've found out how to write files 我发现了如何写文件

# Open for writing
open(MYFILE,">$filepath/$filename") || "> ERROR: Couldn't open file for writing\n";
print MYFILE $result;
close MYFILE;

And also how to get the time as a string 还有如何获取时间作为字符串

my ($sec, $min, $hour, $day, $mon, $year) = localtime();
$LAST_RUN_DATETIME=strftime( "%Y/%m/%d %T", $sec, $min, $hour, $day, $mon, $year);

Thanks for your time 谢谢你的时间

I would write something like this probably: 我可能会这样写:

open(my $handle, ">", "$filepath/$filename") or die "Couldn't open $file";
print $handle scalar(localtime());
close($handle);

and to read it in is just: 而阅读它只是:

open(my $handle, "$filepath/$filename") or die "Couldn't open $file";
my $timestamp = <$handle>;

That timestamp will be in this form: (day of the week) mmm (day of the month) hh:mm:ss yyyy 时间戳将采用以下格式:( (day of the week) mmm (day of the month) hh:mm:ss yyyy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM