-2

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);
        }
    }
}
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Nano HE
  • 9,109
  • 31
  • 97
  • 137
  • 1
    It depends on what you want to compare. In other words, you need to know what you want to do before you can figure out how you're going to do it. – Anon. Dec 22 '09 at 02:13
  • 1
    What are you comparing against to determine if one of the files has changed? – Anon. Dec 22 '09 at 02:18
  • @Anon. Is the 'eq' operation meaninful? i found eventhough i didn't change anything for the three files(sour,desc,script), the _mtime always can't be equal; – Nano HE Dec 22 '09 at 02:22
  • @Anon. I want to monitor any of the three files when they changed. – Nano HE Dec 22 '09 at 02:23
  • 1
    And how will you know when that happens? Magic? You need something else to compare them against to determine if they've changed (usually, you'd compare them against where you'd put the results of your scan). – Anon. Dec 22 '09 at 02:26
  • @Anon. i understand the Stat function now. I ignored several lines in the sample script. It will generate some middle variable value to compared with the _mtimes value. THANK you so much. – Nano HE Dec 22 '09 at 02:50

1 Answers1

2

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 >

Community
  • 1
  • 1
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • @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 Dec 22 '09 at 03:17
  • 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 Dec 22 '09 at 09:20