Using Directories in Perl
Handling Files and Directories in Perl – Part 4
Foreword: In this part of the series, I explain how to change directory, change root directory, create directory, remove directory and list the files of a directory.
By: Chrysanthus Date Published: 27 Aug 2015
The working or current directory is the directory that has your Perl script. It is represented by a dot or its name with path. The parent directory is identified by two dots or its name with path. Use your operating system to create the directories of the following directory path:
Place your Perl script in the dir2 subdirectory. To execute your script at the console, you would type:
and press Enter, to go to the directory first.
To change directory in Perl, there is a function called, chdir. This function returns true on success and false on failure. If you type:
in your Perl code, chdir will lead you to your current directory, which in this case, causes no change because the Perl script is in the current directory. Note that the dot is in quotes. To go to the parent directory, you would type:
To go to any directory, anywhere in the disk, use the path of the directory. In Perl the directory names in the path are separated by forward slashes and not backslashes (it does not matter the operating system in use). To go to dir3 from any directory in the disk, you would type:
For the directories in the path, there should be no space in the name. It does not matter whether the name is in lower or upper case; that is, the names are not case sensitive.
This is the function to create a directory. The function returns true on success and false on failure. If it returns false, it sets the special variable, $! to the error (errno). You can create a subdirectory to the working directory. The following statement creates dir4 in dir3, while the working directory is dir2:
You create only one directory at a time. So, to create a directory, folder1 in the directory, temp that already exists in the root directory, type:
This is the function to delete (remove) a directory. In order for the directory to be removed, it must be empty (delete all its content first). If the function succeeds it returns true. If it fails, it returns false and sets the special variable, $! to the error (errno). Only one directory is removed at a time. The following statement removes dir4, while the working directory is dir2.
Listing all the files and Immediate Subdirectories of a Directory
The following statement will copy the names of all the files into the array, one filename to an array cell.
my @arr = glob("*.*");
The files are copied in alphabetical order. glob("*.*") does not copy the directory names. If you want to copy the filenames and directory names, use glob("*") as follows:
my @arr = glob("*");
Note the use of "*.*" for files only; and the use of "*" for files and directories. The filenames and directory names are copied as one list, mixed, in alphabetical order.
The following code segment will display the list of filenames and directory names of the working directory, in one line at the console:
my @arr = glob("*");
print $_ . " " foreach (@arr);
The second line in the code is a simple foreach (modifier) statement. $_ holds the value of each array cell (element). " " in the second line creates space between listed names. The dot between $_ and " " concatenates the two strings.
The above functions are for the working directory. If you want a different directory, then you have to precede the asterisks with the path as the following illustrates:
my @arr = glob("c:/dirA/dirB/dirC/*.*");
my @arr = glob("c:/dirA/dirB/dirC/*");
This function makes the named directory the new root directory for all further pathnames that begin with a / by your program (and all its children). It does not make the new directory the working directory (working directory stays the same). Consider the following path:
Now, c:/ is the root directory. In Perl you can access the root directory by simply typing /.
You can make any directory the root directory, so that its subdirectories can be accessed beginning with /. To make dirB the root directory, type:
From that you can then go on to type,
my @arr = glob("/dirC/*");
and all the file names and directory names in dirC will be copied to the array.
The Functions with Error Handling
In this section, I say how the error message in $! can be taken into consideration. The mkdir function would be typed as follows:
print "Error: " . $! if !mkdir("dir3/dir4");
This is a simple statement with the if statement modifier. Note that the mkdir function is negated with the ! operator. So, if the function returns false in the if-condition, the error message in $! would be printed.
The rmdir function would be typed as follows
print "Error: " . $! if !rmdir("dir3/dir4");
This is a simple if modifier statement with the rmdir function negated in the if-condition. So, if the directory is not removed, the error message of $! would be printed.
I have talked about 4 directory functions in this tutorial. Two of the functions do not use, $! . You do not have to open a directory in order to use any of the 4 functions.
That is it, for this part of the series. We stop here and continue in the next part.
Related LinksPerl Basics
Perl Data Types
Perl References Optimized
Handling Files and Directories in Perl
Perl Object Oriented Programming
Perl Regular Expressions
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
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library