2009-12-22 40 views
-2

我在下面找到了一些示例腳本「stat」用法。如何比較Perl統計信息返回的mtimes?

$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? 
} 

謝謝。

[updated 0]

eg eg below style。當我調試到腳本。我發現script_ltime值和dest_file_mtime值不會是平等的。

if ($script_ltime eq $dest_file_mtime) // eq used 
{ 
    printf "No $this_file Scan Needed\n"; 
    exit(0); 
} 

順便說一句,如果我而不是腳本與風格貝沃。我發現即使我修改了我的腳本。腳本仍然不會再次掃描。對於dest_file_mtime值,始終大於source_mtime值。

if ($dest_file_mtime gt $source_mtime) // gt used 
{ 
    printf "No $this_file Scan Needed\n"; 
    exit(0); 
} 

爲什麼我不知道使用eq或gt。以及哪種風格更適合「當我更改三個文件中的一個時,腳本將始終掃描所需內容。」

[更新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); 
     } 
    } 
} 
+1

你有什麼問題嗎? – 2009-12-22 02:04:36

+1

這取決於你想要比較的東西。換句話說,你需要知道你想要做什麼,然後才能知道你將如何去做。 – 2009-12-22 02:13:50

+0

@Alexandr。以上更新。謝謝。 – 2009-12-22 02:16:05

回答

2

你選擇了錯誤的比較操作符。

eqgt是字符串比較運算符。由於stat返回一個整數,你必須使用整數比較:

eq應該==

gt應該>

+0

@Alan,你好。在參考我的Perl書後。我發現了這樣的信息:「Perl可以像列表一樣將列表索引到列表中,這是一個列表切片」這本書說它是一個列表切片。沒有提到返回mtime類型。當我與KomodoIDE混合。我也找不到mtime值類型。 IDE在Locals子窗口的TYPE列中顯示NOTHING。謝謝。 – 2009-12-22 03:17:52

+0

@這意味着'eq'和'=='都能正常工作嗎?謝謝 – 2009-12-22 03:18:59

+0

Hi Nano。 'eq'和'=='是不同的。 'eq'確實**字符串比較**。而==不會**數字比較**。 Perl是一種_loosely類型的語言_。整數,我的意思是沒有小數的數字。 – 2009-12-22 09:20:30