What is dirname() method in Perl?
Overview
The dirname() method in Perl is used to get the directory of the folder name of a file.
Syntax
dirname(file_path)
Parameters
file_path: The path of the file we want to get its directory name.
Return value
This method returns the folder or directory name or a symbolic link that contains the file.
Example
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";
Explanation
-
Line 2: We import the
FIle::basenamesubroutine, 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.