當前位置:首頁 » Perl » perl rewinddir

perl rewinddir

perl rewinddir函數例子,rewinddir函數實例代碼 - 複位開始由DIRHANDLE指定目錄內的當前位置。

語法

rewinddir DIRHANDLE


定義和用法

複位開始由DIRHANDLE指定目錄內的當前位置。

返回值

  • 0 - 失敗

  • 1 - 成功

例子

在/tmp目錄,並嘗試下麵的例子:

#!/usr/bin/perl -w
#by www.gitbook.net

# Open the current directory
opendir(DIR, ".");

# Print all of the directory entries.
print("1st Time: \n");
map( print("$_ \n") , readdir(DIR));
print("\n");

# Print message verifying that there are
# no more directory entries to read.
print("The last file has already been read!\n\n")
 unless readdir(DIR);

# Go back to the beginning.
rewinddir(DIR);

# Print all of the directory entries again.
print("2nd Time: \n");
map( print("$_ \n") , readdir(DIR));
print("\n");

closedir(DIR);

這將產生以下結果:(檢查/tmp目錄)

1st Time:
.
..
testdir
The last file has already been read!
2nd Time: 
.
..
testdir