2017-07-03 66 views
-2

我想重複以下命令,如果文件$grib不存在,直到它存在..什麼是引入循環以反覆檢查文件是否存在的最佳方法是什麼?使用如果與循環

$grib = "model.grb"; 

if (-e $grib) { 
    print "File $grib exists"; 
} 
else { 
    print "without file $grib"; 
} 
+0

帶'while'循環。 – simbabque

回答

5

您需要一個while循環。

while (!-e $grin) { 
    # do stuff... 
} 

print "file $grib exists\n"; 

請參閱compound statements in perlsyn

+4

除非你真的不應該這樣做,因爲你會像CPU一樣狠狠地抖動,因爲它只會每隔幾微秒重複檢查一次。那裏的「睡眠1」會有很大的改善。 – Borodin

+0

@Borodin,謝謝你的建議... –

+1

另請參閱[Filesys :: Notify :: Simple](https://metacpan.org/pod/Filesys::Notify::Simple)。 –