當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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 rewinddir Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。