Skip to content
Related Articles

Related Articles

Improve Article

Perl | Directories with CRUD operations

  • Last Updated : 09 May, 2019
Geek Week

Perl is universal and cross-platform programming language mainly used for text manipulation and used in developing many software application like web development, graphical user interface application etc. It is preferred over other programming languages as it is faster, powerful and Perl has a lot of shortcuts which helps in writing a quick script making it take less time for writing.
A directory is used in programming languages to store values in the form of lists. A directory is quite similar to a file. Just like a file, the directory also allows performing several operations on it. These operations are used for the modification of an existing directory or creation of a new one.

Different operations that can be performed on a Directory are:

  1. Creation of a new Directory
  2. Opening an existing Directory
  3. Reading content of a Directory
  4. Changing a Directory path
  5. Closing a Directory
  6. Removing a Directory
Creating a Directory

 
To create a directory mkdir(PATH, MODE) is used. This function helps to create a new directory, if the user wants to check that the file already exists, it can be done by -e function. The path is been set by PATH using the mode which is specified by MODE function.
Example:




#!/usr/bin/perl  
  
# Path of the directory
my $directory = 'C:\Users\GeeksForGeeks\Folder\Perl';  
  
# Creating a directory in perl  
mkdir($directory) or die "No $directory directory, $!";  
print "Directory created \n"

Output:



 

Opening a directory

 
To open a directory in Perl a short function opendir DIRHANDLE, PATH is used. The PATH here is the path of the directory to be opened.
Example :






#!/usr/bin/perl  
  
my $directory = 'C:\Users\GeeksForGeeks\Folder';  
opendir (DIR, $directory) or die "No directory, $!";  
while ($file = readdir DIR) 
{  
    print "$file\n";  
}  
closedir DIR;

OutPut:

 

Read Directory in Scalar and List Context

 
Reading a directory is a common task as one has to every time read what is stored in the files to run the code or to understand the code. To read a directory, readdir DIRHANDLE is used. There are two ways a user can read the directory, i.e. in list context and scalar context.
In list context, the code returns all the rest of the entries in the directory and if the entries are empty in the directories then the undefined values will be returned in scalar context and the empty list in the list context.
Scalar context:
Example:




#!/usr/bin/perl  
use strict; 
use warnings; 
use 5.010; 
  
# Path of the directory
my $directory = shift // 'C:\Users\GeeksForGeeks\Folder'
  
# Opening the directory
opendir my $dh, $directory or 
die "Could not open '$directory' for reading '$!'\n"
  
# Printing content of the directory
while (my $content = readdir $dh
    say $content
  
# Closing the directory
closedir $dh

Output:

List context:




#!/usr/bin/perl  
use strict;  
use warnings;  
use 5.010;  
  
# Path of the directory
my $directory  = shift // 'C:\Users\GeeksForGeeks\Folder';  
  
# Opening the directory
opendir my $dh, $directory or 
die "Could not open '$directory' for reading '$!'\n";  
  
# Reading content of the file
my @content = readdir $dh;  
  
# Printing content of the directory
foreach my $content (@content
{  
    say $content;  
}  
  
# Closing the directory
closedir $dh;

Output:

 

Changing a directory

 
To change a directory chdir() function is used. This function helps to change the directory and send it to a new location. chdir() function when called with a script, changes the directory for the rest of the script. The directory on the terminal will not be changed if this function is called within a script. On the other hand, when called directly with a new directory path, changes reflect at the same time in the terminal.
Example 1: When chdir() is called within a script




#!/usr/bin/perl
  
# Module to return 
# current directory path
use Cwd;
  
# Path of new directory
$directory = "C:/Users";
  
# Printing the path of current directory
# using cwd function
print "The current directory is ";
print(cwd);
print "\n"
  
# Changing the directory using chdir function
chdir($directory) or 
      die "Couldn't go inside $directory directory, $!";
  
# Printing the path of changed directory
print "Directory has been changed to $directory\n";
print "The current directory is ";
print(cwd);
print "\n"

Output

 
Example 2: When chdir() is called directly on the terminal

 

Closing a directory

 
To close a directory closedir DIRHANDLE is used. Here, DIRHANDLE is the handle of the Directory with which it is opened using opendir function.
Example:




#!/usr/bin/perl
  
# Directory which is to be opened
$dirname = "C:/Users/GeeksForGeeks";
  
# Opening the directory 
# using opendir function
opendir (dir, $dirname) || die "Error $dirname\n";
  
# Printing content of the directory
# using readdir function
while(($filename = readdir(dir))) 
{
    print("$filename\n");
}
  
# Closing the directory using closedir 
closedir(dir);

Output:

 

Removing a Directory

 
Removing a Directory can be done by using rmdir function. This function removes the specified directory by FILENAME only if that directory is empty, if it succeeds it returns true, otherwise false.
Example:




#!/usr/bin/perl  
$directory = "C:/Users/GeeksForGeeks/Folder/Perl";  
  
# This removes perl directory  
rmdir($directory ) or
      die "Couldn't remove $directory directory, $!";  
        
print "Directory removed \n";  

Output:





My Personal Notes arrow_drop_up
Recommended Articles
Page :