views:

369

answers:

1

Hello.

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 Needed\n";
      exit(0);
     }
    }
}
+2  A: 

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 >

Alan Haggai Alavi
@Alan, Hello. After refer to my perl book. i found the information belwo: "Perl can index into a list as if it were an array. This is a list slice" The book said it is a list slice. Not mentioned the return mtime type. When i dubug with KomodoIDE. i also can't find the mtime value type. The IDE show NOTHING at the TYPE column from Locals sub window. thank you.
Nano HE
@Does that mean both 'eq' and '==' work well? thank you
Nano HE
Hi Nano. `eq` and `==` are different. `eq` does **string comparison**. Whereas, == does **numeric comparison**. Perl is a _loosely typed language_. By integers, I meant numerals without decimals.
Alan Haggai Alavi