Broad Network


Processing Immediate Subdirectories in Perl

Handling Files and Directories in Perl – Part 5

Perl Course

Foreword: In this part of the series, I explain how to scan the immediate subdirectory entries, how to access a directory, and how to open and close a directory anywhere in the disk.

By: Chrysanthus Date Published: 27 Aug 2015

Introduction

This is part 5 of my series, Handling Files and Directories in Perl. In this part of the series, I explain how to scan the immediate subdirectory entries, how to access a directory, and how to open and close a directory anywhere in the disk. You should have read the previous parts of the series before reaching here, as this is a continuation.

DIRHANDLE
The DIRHANDLE is a kind of filehandle, but it is for directories and not files. DIRHANDLE stands for Directory Handle. A filehandle is one of the types of Perl variables. A DIRHANDLE is used for opening and closing a directory. It is used to access the opened directory. It is used for scanning immediate subdirectory entries.

Opening and Closing a Directory
To open a directory means to have a reference in memory and have an association between the directory reference in memory and the directory in the disk. When you close the directory, you break the association.

The directory opened, must not necessarily be a subdirectory to the working directory. It can be anywhere in the disk. The directory entries scanned are subdirectories to the opened directory.

To open a directory, you type something like:

    opendir(dHandle, "c:/dirA/dirB") or die "cannot open directory : $!";

The second argument is the path and directory to be opened. If the directory is a subdirectory to the working directory, then you can type something like,

    opendir(dHandle, "dirC/dirD") or die "cannot open directory : $!";

where dirD is the directory of interest.

Any directory opened, must be closed (after). To close a directory, use the syntax:

    closedir DIRHANDLE

Both the opendir and closedir functions return true on success and false on failure.

Functions used with Opened Directory
The following functions can be used when the directory is opened.

    readdir, seekdir, telldir, rewinddir

For the rest of this tutorial, I explain these functions. When the directory is closed, you cannot use these functions again.

readdir
Directory entries typically consist of files and subdirectories. Among directory entries, a dot means the current directory and a double dot means the parent directory.

The readdir function uses the directory handle to return a list of subdirectory entries and filenames, from the position of the directory pointer to the end of the directory. The list should be received by an array. Immediately after opening the directory, readdir will return all the entries in the directory (because the directory pointer is positioned at the start initially).

Create the following path and place files of your choice in the directories: c:/dirA/dirB/dirC/dirD. Try the following code:

use strict;

    opendir(dHandle, "/dirA/dirB") or die "cannot open directory : $!";

    my @dirList = readdir(dHandle);

    print $_, ' ' foreach @dirList;

    closedir dHandle;

The directory of interest is dirB. I tried the code in my computer and the first entry was a dot, for the current directory; the second entry was a double dot, for the parent directory. The rest of the entries where subdirectories and files, of the current directory. The subdirectories were arranged in alphabetical order and the files were also arranged in alphabetical order.

The syntax of the readdir function is:

    readdir DIRHANDLE

Note: In scalar context (that is when the receiving assignment variable is a scalar), readdir returns just the next entry.

Whether in list or scalar context, the readdir function moves the directory pointer one entry at a time, toward the end of the directory entries.

If the directory is not opened, in order to obtain the entries, you have to use glob("*").

telldir
The syntax for this function is:

    telldir DIRHANDLE

This function uses the directory handle to return the current position of the directory pointer. This kind of returned value is not usually used on its own. So, it is returned to a variable, and it is the variable that is used. When a directory is just opened, the directory pointer points to the first element of the directory entries.

seekdir
The syntax for this function is:

    seekdir DIRHANDLE,POS

This function sets the current (new) position of the directory pointer. POS is the value (variable) returned by the telldir function.

rewinddir
This function takes back the directory pointer to the beginning of the directory entries (to point to the first element). The syntax is:

    rewinddir DIRHANDLE

Now, read and try the following code:

use strict;

    opendir(dHandle, "/dirA/dirB") or die "cannot open directory : $!";

    my $dirList = readdir(dHandle);
    $dirList = readdir(dHandle);
    $dirList = readdir(dHandle);

    my $pos = telldir(dHandle);

    rewinddir(dHandle);
    print $dirList; print ' ';

    seekdir(dHandle,$pos);

    $dirList = readdir(dHandle);
    print $dirList;

    closedir dHandle;

I tried the code in my computer and I had the first and the fourth entries. In my computer, the first entry is a dot.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message