Perl Tutorials - Herong's Tutorial Examples - v6.01, 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.
closedir(dir_handle);
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
Hard References - Addresses of Memory Objects
Objects (or References) and Classes (or Packages)
Typeglob and Importing Identifiers from Other Packages
String Built-in Functions and Performance
File Handles and Data Input/Output
►Open Directories and Read File Names
►opendir() - Open Directory to Read File Names
opendir.pl - Sample Program to Read Directories
DirTree.pl - Displaying the Directory Tree
DirGrep.pl - Searching Text in Directory Files
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
SOAP::Lite - SOAP Server-Client Communication Module
Perl Programs as IIS Server CGI Scripts
CGI (Common Gateway Interface)
XML-RPC - Remote Procedure Call with XML and HTTP
RPC::XML - Perl Implementation of XML-RPC
Integrating Perl with Apache Web Server
CGI.pm Module for Building Web Pages
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary