Perl rewinddir 函式



描述

此函式將 DIRHANDLE 指定目錄中的當前位置重置到目錄的開頭。

語法

以下是此函式的簡單語法 -

rewinddir DIRHANDLE

返回值

此函式在失敗時返回 0,在成功時返回 1。

示例

以下是顯示其基本用法的示例程式碼 -

#!/usr/bin/perl -w

# 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
perl_function_references.htm
廣告
© . All rights reserved.