How can I compare mtimes returned by Perl's stat?
I found some sample scripts "stat" usage below.
$source_mtime = (stat($source_file))[9];
$dest_file_mtime = (stat($dest_file))[9];
$script_mtime = (stat($this_file))[9];
if (-e $dest_xml_file)
{
if ($dest_file_mtime gt $source_mtime) // gt used
{
printf "No $this_file Scan Needed\n";
exit(0);
}
# OR the style below
if ($script_ltime eq $dest_file_mtime ) // eq used
{
printf "No $this_file Scan Needed\n";
exit(0);
}
# OR the style below
if ($script_ltime eq $source_mtime ) // eq used
{
printf "No $this_file Scan Needed\n";
exit(0);
}
# or other style?
}
thank you.
[updated 0]
eg the style below. when i debug into the script. I found the script_ltime value and dest_file_mtime value won't be equial.
if ($script_ltime eq $dest_file_mtime ) // eq used
{
printf "No $this_file Scan Needed\n";
exit(0);
}
btw, if i instead of the script with the style belwo. i found even i modified my script. The script still won't be scan again. For the dest_file_mtime value always greater than source_mtime value.
if ($dest_file_mtime gt $source_mtime) // gt used
{
printf "No $this_file Scan Needed\n";
exit(0);
}
Tha't why i confued to use eq OR gt. and which style is better for "When I changed one of the three file, the script will always scan needed."
[updated 1]
if (-e $dest_file) {
open(DEST_FILE, "$dest_file") ;
$_ = <DEST_FILE>;
close DEST_FILE;
if (/^\/\*([\w]+)\/\/([\w]+)\*\//) { # ignored by me code here
$ltime = $1; # middle variable value assignment
$script_ltime = $2;
if (($ltime eq $mtime) && # eq operator is meaningful
($script_ltime eq $script_mtime)) {
printf "No $this_file Scan Neede开发者_开发技巧d\n";
exit(0);
}
}
}
You have selected the wrong comparison operators.
eq
and gt
are string comparison operators. Since stat
returns integers, you have to use integer comparison:
eq
should be==
gt
should be>
精彩评论