Introduction to Perl mkdir
mkdir function in Perl stands for make directory on the specified path. If we want to create a directory on any path in our system or any whereon the network, we can use it to create it. It takes two params while using this function in Perl. In the mkdir function, we have to specify the name of the directory as well by which we want our directory to be created. mkdir function takes one of the params as MODE where we mentioned our MODE. In the coming section, we will discuss more about the mkdir function in Perl to understand it better for beginners.
As we know, it is a function, and it takes two parameters as the input param while using it. Let’s see its syntax in more detail for better understanding and its usage see below;
1. mkdir FILENAME,MODE: This is the syntax given by the official Perl documentation. It is taking to param named as FILENAME and MODE. In the coming section, we will see its signature in more detail.
2. mkdir FILENAME: This is also an official syntax given by the Perl documentation. Here we just pass the directory name, and MODE is taken as default. In the coming section, we will see its signature in more detail.
Let’s see one sample practice syntax for beginners for its better understanding see below;
mkdir $your_directory_name, MODE;
So it will create the directory in the specified path mentioned
How does the mkdir function work in Perl?
As we already know now, the mkdir function stands for make directory, and it is used to make the directory on the specified path in Perl. It is an inbuilt function available in Perl, so we do not need to include any external library to use it inside our program. In many scripting languages, we have this function available, and it also works in the same way as Perl, ‘mkdir’ generally and most commonly referred to create a directory on the system or network by writing the script. In the section, we will see more closely its signature as per the Perl documentation; for better usage, see below;
Method Signature ;
1. mkdir EXPR,MODE: This function takes two parameters as the input param in Perl. after given both the params value, it will create a directory on the path given as the input.
a) EXPR / FILENAME: By using this parameter, we can specify or defined our path where we want our directory to be created on the system. We can separately create this or assign it by using “” simpl string literal in Perl. Let’s see how to define this in Perl to make the mkdir function works as expected see below;
$directoryname = "/some/path";
b) MODE: This parameter is used to define the MODE in the Perl mkdir function. If the MODE is not specified, then it will take up the default MODE, which is already defined in Perl; the default MODE is 0755.
2. Return Type: mkdir function in Perl return us the Boolean value on success and failure. If the directory is created successfully, it will return TRUE; if the directory is not created successfully or is a failure, it will return FALSE.
Now we will see one practice example for beginners to understand how to use this function while programming; also, it will explain the internal working of this function. See below;
print "Demo to show mkdir function working in perl by using a simple example \n";
$result = mkdir $mydir, 0755;
print "result is : $result\n";
Here is one sample example, firstly we are creating one variable named as ‘mydir’; this variable will hold the path of the directory where it should be created. After this, we use trying to use the mkdir function with both the parameters specified. The first parameter we are passing as a directory path, which is the ‘mydir’ variable. Second is the MODE which we are passing as default. Also, we are holding its result into a ‘result’ variable, which will tell us it is a success or not. One success it will return 1, and on failure, it will return 0. We can use this result to make any conditions based on the requirement. Also, we can throw an exception on failure of the directory creation because it may be in the next piece of code; we are using that directory to store some files. In the next part of the article, we will see some practical example for the mkdir function in Perl.
In this example, we are trying to use the mkdir function in Perl; we have not imported any of the external libraries to use it because it is an inbuilt function. We have created several directories using this example and printing the result, whether it is a success or failure. This is a simple example for beginners to start with the mkdir function in Perl.
print "Demo program to show mkdir function in Perl \n";
$mydir1 = "/tmp/directory1";
$mydir2 = "/tmp/directory2";
$mydir3 = "/tmp/directory3";
$mydir4 = "/tmp/directory4";
$mydir5 = "/tmp/directory5";
$mydir6 = "/tmp/directory6";
print "calling mkdir function calls in Perl ... \n";
$result1 = mkdir $mydir, 0755;
$result2 = mkdir $mydir, 0755;
$result3 = mkdir $mydir, 0755;
$result4 = mkdir $mydir, 0755;
$result5 = mkdir $mydir, 0755;
$result6 = mkdir $mydir, 0755;
print "Prinitng result after mkdir function calls in Perl ... \n";
print "Result one is : $result1\n";
print "Result two is : $result2\n";
print "Result three is : $result3\n";
print "Result four is : $result4\n";
print "Result five is: $result5\n";
print "Result six is : $result6\n";
By using the mkdir function, we can easily create a directory while programming. It is very easy to write, readable and easy to understand as well. This function is not so complex because it just takes the path and name of the directory where we want it to be created. IF mode is not specified, it will take the default one.
This is a guide to Perl mkdir. Here we discuss the How does the mkdir function work in Perl along with the example and output. You may also have a look at the following articles to learn more –