Perl Tutorials - Herong's Tutorial Examples - Version 5.40, by Dr. Herong Yang
opendir() - Open Directory to Read File Names
This section describes Perl built-in functions, opendir() and readdir(), to open a file directory and read its file names and sub-directories.
There are 3 functions in Perl that help you to open a directory and read its file names:
1. opendir() - A function to open the specified directory and associate it to a directory handle with the following syntax:
rc = opendir(dir_handle, expression);
where "expression" specifies the path name of the directory to be opened, and "dir_handle" is the variable name of the new directory handle. opendir() returns true, if operation is successful.
Directory handle has its own name space. Same name can co-exist in different name spaces.
2. readdir() - A function to access the content of the specified directory handle. If the function is called in a scalar context, it will return the next entry. If the function is called in an array context, it will return the rest of entries. Examples of calling readdir() in scalar and array contexts:
$s = readdir(dir_handle); @a = readdir(dir_handle);
3. closedir() - A function to close the directory associated with the specified directory handle.
Table of Contents