Perl Directories

Different operating systems have different commands to look at the files list within a directory. For example, 'li' command is used for Linux and 'dir' command is used for Windows.

But we can also open a directory in Perl using Perl directory functions. Here, Directory handling is quite similar to file handling.

Perl provides two alternatives for directories: file globbing and directory functions. File globbing is usually used for a similar subset of files whereas directory functions provide more options.

Directory Functions

In the given table, we have listed some of the directory functions.

Function Usage
opendir DIRHANDLE, EXPR It opens a directory.
readdir DIRHANDLE It reads a directory.
rewinddir DIRHANDLE It positions pointer to the beginning.
telldir DIRHANDLE It returns current position of the dir
seekdir DIRHANDLE, POS It points pointer to POS inside dir.
closedir DIRHANDLE It closes a directory.

Open/Display Directory

To open a directory in Perl, there is a function opendir. This function returns true on success or false on failure.

Setting $! with actual error message will display a reason for the failure.

Syntax
opendir(DIR, 'dirName') or die;
Example
snippet
#!/usr/bin/perl
my $directory = '/users/rookienerd';
opendir (DIR, $directory) or die "Couldn't open directory, $!";
while ($file = readdir DIR) {
  print "$file\n";
}
closedir DIR;
Perl Directory 1

Create Directory

To create a new directory in Perl, there is a function mkdir. You need required permission to create a directory.

Syntax
mkdir(dirName) or die;
Example
snippet
#!/usr/bin/perl
my $directory = '/users/rookienerd/perl';
#creating directory perl
mkdir( $directory ) or die "Couldn't create $directory directory, $!";
print "Directory created successfully\n";
Output
Directory created successfully

Given snapshot shows created directory perl in the /users/rookienerd directory list.

Perl Directory 2

Read Directory in SCALAR context

To read content of a directory, function readdir is used. In scalar context, this function will return each item of a directory one by one. Once everything is read out, it will return undef.

Syntax
while (my $directory = readdir $dh) {
    say $directory;
}
Example
snippet
use strict;
use warnings;
use 5.010;
my $directory = shift // '/users/rookienerd';
opendir my $dh, $directory or die "Could not open '$directory' for reading '$!'\n";
while (my $content = readdir $dh) {
    say $content;
}
closedir $dh;
Perl Directory 3

Read Directory in LIST context

In list context, readdir function will return all the content of directory in one statement. So it uses more memory.

Syntax
my @directory = readdir $dh;
foreach my $directory (@directory) {
    say $directory;
}
Example
snippet
use strict;
use warnings;
use 5.010;
 my $directory  = shift // '/users/rookienerd';
opendir my $dh, $directory or die "Could not open '$directory' for reading '$!'\n";
my @content = readdir $dh;
foreach my $content (@content) {
    say $content;
}
closedir $dh;
Perl Directory 4

Remove Directory

To remove a directory in Perl, there is a function rmdir. You need required permission to remove a directory. The directory which you want to remove should be empty before removing it.

Syntax
rmdir('dirName') or die;
Example
snippet
#!/usr/bin/perl
$directory = "/users/rookienerd/perl";
# This removes perl directory from /tmp directory.
rmdir( $directory ) or die "Couldn't remove $directory directory, $!";
print "Directory removed successfully\n";
Output
Directory removed successfully

Change Directory

To change a directory in Perl, there is a function chdir. To change a directory and go inside a new directory you need required permission.

Syntax
chdir('dirName') or die;
Example
snippet
#!/usr/bin/perl
$directory = "/users/";
# TThe directory changes to users.
chdir( $directory ) or die "Couldn't go inside $directory directory, $!";
print "Diretory has been changed to $directory\n"
Output
Diretory has been changed to /users/

Close Directory

To close a directory in Perl, there is a function closedir. This function officially shut down the connection between directory handle and directory. Although, closing directory is not mandatory in Perl because directory will be automatically closed when variable goes out of scope.

Syntax
closedir $dh;
	OR
closedir DIR;

Perl Opening Directory using File Globbing

To open all files in a directory matching a specific pattern, use Perl filename glob matching pattern syntax.

We'll display the list of all the files having extension .docx in a directory.

snippet
#!/usr/bin/perl -w
opendir(DIR, ".");
@files = grep(/\.docx$/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
   print "$file\n";
}
Perl Directory 5

In the above output, all the files are read in the directory, but grep only passes files with .docx extension.

Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +