The dirname()
method in Perl is used to get the directory of the folder name of a file.
dirname(file_path)
file_path
: The path of the file we want to get its directory name.
This method returns the folder or directory name or a symbolic link that contains the file.
Let’s look at the code below:
# import sub routineuse File::Basename;# get file paths$f1 = dirname("hi/main.perl");$f2 = dirname("main.perl");$f3 = dirname("home/code/main.perl");$f4 = dirname("~/main");# print directoriesprint "$f1";print "\n$f2";print "\n$f3";print "\n$f4";
Line 2: We import the FIle::basename
subroutine, which helps us parse file paths.
Lines 5 to 8: We create some file paths and get their directory names using the dirname()
method.
Lines 11 to 14: We print the directories to the console.