Perl readdir Function



Description

This function returns the next directory entry from the directory associated with DIRHANDLE in a scalar context. In a list context, returns all of the remaining directory entries in DIRHANDLE.

Syntax

Following is the simple syntax for this function −

readdir DIRHANDLE

Return Value

This function returns the next directory entry from the directory associated with DIRHANDLE in a scalar context. In a list context, returns all of the remaining directory entries in DIRHANDLE.

Example

Following is the example code showing its basic usage −

#!/usr/bin/perl -w

$dirname = "/tmp";

opendir ( DIR, $dirname ) || die "Error in opening dir $dirname\n";
while( ($filename = readdir(DIR))) {
   print("$filename\n");
}
closedir(DIR);

When above code is executed, it produces the following result −

.
..
testdir
perl_function_references.htm
Advertisements