Perl | Directories with CRUD operations
Last Updated :
09 May, 2019
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:
- Creation of a new Directory
- Opening an existing Directory
- Reading content of a Directory
- Changing a Directory path
- Closing a Directory
- 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:
Perl
#!/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 :
Perl
#!/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:
Perl
#!/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:
Perl
#!/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
Perl
#!/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:
Perl
#!/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:
Perl
#!/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:
Similar Reads
Servlet - CRUD Operation with Example
CRUD means Create, Read, Update and Delete. These are the basic important operations carried out on the Database and in applications. We will build a simple User registration application using a Servlet, MYSQL, and JDBC for demonstration. In this example, we will be able to create users, read users,
10 min read
Copy Multiple Directories with One Command
System administration and development must have their files and directories well administered. One such way of doing this that is really effective is the task of copying multiple directories together, which actually saves one from consuming a huge amount of time and decreases the possibility of erro
7 min read
Perl | Finding Files and Directories
For traversing a directory tree in Perl, there are several ways/methods. Traversing can be performed through function calls opendir and readdir which are a part of Perl programming language. Traversing files and directories in Perl can also be done through File::Find module which comes with the Perl
3 min read
CRUD Operation in Ruby on Rails
In Ruby on Rails, CRUD stands for Create, Read, Update, and Delete the four basic operations for managing data in most web applications. Rails makes implementing CRUD functionality very straightforward using its MVC (Model-View-Controller) architecture.Table of ContentWhat is CRUD?Importance of CRUD
7 min read
Python - Copy Directory Structure Without Files
In this article, we will discuss how to copy the directory structure with files using Python. For example, consider this directory tree: We have a folder named "base" and inside have we have one folder named "Structure". "Structure" has some folders inside which also contain some files. Now we have
3 min read
C# Program For Listing the Files in a Directory
Given files, now our task is to list all these files in the directory using C#. So to do this task we use the following function and class: DirectoryInfo: It is a class that provides different types of methods for moving, creating, and enumerating through directories and their subdirectories. You ca
2 min read
What is a Directory Traversal Attack?
Directory Traversal Attack is a kind of Brute-force attack which will give potential access to restricted files and directories. This attack can also tell the attacker about the directory structure of the web application. It is very important to make web applications secure by giving protection to w
6 min read
Get and Set Working Directory in R
In this article, we are going to see how to get and set a working directory in R Programming Language. How to Get Working directory: getwd(): The getwd() method is used to gather information about the current working pathname or default working directory. This function has no arguments. It returns a
2 min read
How to Ignore Folders and Directories in Git with .gitignore?
Git is a powerful and widely-used version control system that allows developers to track changes in their codebase, collaborate seamlessly, and maintain a robust history of their projects. It was created by Linus Torvalds in 2005, Git has become widely accepted for version control in the software de
4 min read
Perl | Accessing a Directory using File Globbing
In Perl, a directory is used 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. A directory ca
2 min read