Perl provides several functions that can be used to create directories, change directories, open directories, and so on. Here are some common ones :
The mkdir command is used to create a new directory in the current file systesm. When you create a new directory using this function, you can specify what permissions you want the directory to have. mkdir will not overwrite an existing directory.
dirname will be the name of the newly created directory. mode is where you specify the permissions using the number systems you've already seen. It is a zero, then the three numbers discussed on the CHMOD page.
As like most functions, a TRUE value is returned upon success. A simple IF ELSE statement can be used to know if the creation was good or not.
The chdir command changes the current working directory to a new one after the program has been initialized.
dirname is the complete path to the new working directory. The following example searches for a file in the (default) current working directory and then changes the working directory and searches for the same file again.
The opendir command opens a directory and associates it to a directory handle. Once you use this function, you can perform other operations on it such as reading all its contents.
dir_handle is the name you associate with the opened directory just like the OPEN function and FILEHANDLE of a file. Also similar to the file aspect is closing the directory...
The reddir command reads the first file or subdirectory of an open dir.
The mkdir command is used to create a new directory in the current file systesm. When you create a new directory using this function, you can specify what permissions you want the directory to have. mkdir will not overwrite an existing directory.
| mkdir (dirname, mode); |
dirname will be the name of the newly created directory. mode is where you specify the permissions using the number systems you've already seen. It is a zero, then the three numbers discussed on the CHMOD page.
As like most functions, a TRUE value is returned upon success. A simple IF ELSE statement can be used to know if the creation was good or not.
|
# an attempt is made to create a enw directory called newdir
if (mkdir ("newdir", 0777)){ print "The directory was created successfully! \n"; } else { print "The new directory could not be created because of permisssion or becuase a directory with that name already exists. \n"; } |
The chdir command changes the current working directory to a new one after the program has been initialized.
| chdir (dirname); |
dirname is the complete path to the new working directory. The following example searches for a file in the (default) current working directory and then changes the working directory and searches for the same file again.
|
if (-e "output.txt"){ print "File exists \n"; } else { print "File does not exist \n"; } chdir ('../newdir') || die ("Could not set new directory"); if (-e "output.txt"){ print "File exists \n"; } else { print "File does not exist \n"; } |
The opendir command opens a directory and associates it to a directory handle. Once you use this function, you can perform other operations on it such as reading all its contents.
| opendir (directory_handle, directory); |
dir_handle is the name you associate with the opened directory just like the OPEN function and FILEHANDLE of a file. Also similar to the file aspect is closing the directory...
| closedir (directory_handle); |
The reddir command reads the first file or subdirectory of an open dir.
| readdir (directory_handle); |

