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.
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. |
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.
opendir(DIR, 'dirName') or die;
#!/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;
To create a new directory in Perl, there is a function mkdir. You need required permission to create a directory.
mkdir(dirName) or die;
#!/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";
Given snapshot shows created directory perl in the /users/rookienerd directory list.
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.
while (my $directory = readdir $dh) { say $directory; }
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;
In list context, readdir function will return all the content of directory in one statement. So it uses more memory.
my @directory = readdir $dh; foreach my $directory (@directory) { say $directory; }
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;
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.
rmdir('dirName') or die;
#!/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";
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.
chdir('dirName') or die;
#!/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"
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.
closedir $dh; OR closedir DIR;
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.
#!/usr/bin/perl -w opendir(DIR, "."); @files = grep(/\.docx$/,readdir(DIR)); closedir(DIR); foreach $file (@files) { print "$file\n"; }
In the above output, all the files are read in the directory, but grep only passes files with .docx extension.