######################read()函數中的offset參數含義
read (FILEHANBD, $var, $length, $offset)
read 函數有四個參數,最容易誤解的是$offset
它的含義看官方文檔,解釋是:
An OFFSET may be specified to place the read data at some place in the string other than the beginning. A negative OFFSET specifies placement at that many characters counting backwards from the end of the string. A positive OFFSET greater than the length of SCALAR results in the string being padded to the required size with "\0" bytes before the result of the read is appended.
#####################最佳化技巧 函數的開銷是很大的。
A迴圈地調用一個函數
B在函數中迴圈地進行一個操作
從效能上說,B比A好。
#####################glob的用法--讀取一個目錄下的所有檔案
1. 比如要讀取/home/globtest目錄中的所有檔案,可以這樣寫:@plFiles = glob '/home/globtest/*.*';
上面的glob的用法相當於如下
opendir(Dir,"/home/globtest");#注意這是在Linux的寫法,若在Windows下,應該寫為opendir(Dir,"F:\\home\\globtest");
@Files=readdir(Dir);
closedir(Dir);
foreach $Cur(@Files){
$File="/home/globtest".$Cur;
open(IN,$File);
while(<IN>){
......
}
close(IN);
}
2 . @many = glob "{apple,tomato,cherry}={green,yellow,red}";
把@many數組輸出就能看到會顯示:
1 apple=green
2 apple=yellow
3 apple=red
4 tomato=green
5 tomato=yellow
6 tomato=red
7 cherry=green
8 cherry=yellow
9 cherry=red