perl function mkdir
In Perl, the mkdir
function is used to create a new directory. Here's an example that demonstrates how to use mkdir
:
#!/usr/bin/perl use strict; use warnings; my $dir = "new_directory"; if (mkdir($dir)) { print "Directory created successfully!\n"; } else { print "Failed to create directory: $!\n"; }
In this example, we define a variable $dir
that contains the name of the directory we want to create. We then use the mkdir
function to attempt to create the directory. If the function returns a true value, we print a success message to the console. If the function returns a false value, we print an error message that includes the reason for the failure, which is stored in the special variable $!
.
Note that you may need to include the full path of the directory you want to create if you don't want it to be created in the current working directory. Additionally, the mkdir
function can take an optional second argument that specifies the permissions that should be set on the new directory. If you don't specify this argument, the directory will be created with default permissions.
Here's an example that demonstrates how to specify permissions when creating a directory:
#!/usr/bin/perl use strict; use warnings; my $dir = "new_directory"; my $permissions = 0755; if (mkdir($dir, $permissions)) { print "Directory created successfully!\n"; } else { print "Failed to create directory: $!\n"; }
In this example, we specify the $permissions
variable to contain the numeric value 0755
, which corresponds to the permissions rwxr-xr-x
. We then pass this variable as the second argument to the mkdir
function, which causes the new directory to be created with the specified permissions.