Updated April 12, 2023
Definition of Perl basename function
basename function in Perl is used to fetch the file name from the path. basename function used to deal with the directory, file name, and suffix in Perl. But basename function is mainly deal with the file name, if we want to fetch the directory portion we may have to use other function available in Perl, also basename function takes different parameters to get the file name from the existing path. In the coming section, we will discuss more the basename function available in Perl in detail.
Syntax:
As we discussed this function is used to get the basename name of the file from the path. This is an in build function available in Perl scripting. Let’s discuss its syntax in details to get the more understanding of the function in detail see below;
1)
basename($Your_path);
2)
basename($Your_path, @Your_suffixes);
As you can see in the above syntax mentioned above for basename function in Perl it takes different parameters to return us the path of the file but majorly it will depend upon the parameters we pass as the input here. Let’s see one sample syntax practice for beginners to understand it better.
e.g.:
basename("my/sample/path");
In the above lines of input as you have seen we are passing our path there and it will return us the result. In the coming section we will discuss more about the function working in detail.
How does basename function work in Perl?
As now we know that basename function is used to get the file path from the parameter passed. This function takes input parameters and based on that it will return the name. But this function return the last level of the file path it can be directory itself. It will just return the last level form the path. We can also pass the suffix as the parameter which will be the extension for the file. In this section, we will see the method signature first to understand how does this function works in Perl o get the file name or portion of the file name form the mentioned path see below;
Method signature :
1) basename($Your_path): In this function, we can pass one parameter as the input param. This parameter takes the path of the file as input, after that it will return the result containing the last level from the path as the file name. It can be anything file name or directory whichever appears at the end of the path. Below see the sample example for this function see below;
e.g. :
my $filesample1 = basename("/my/sample/path/for/demo");
As you cans ee in the above sample example we are passing the path of the fie as ‘/my/sample/path/for/demo’ so while retrieving the result it will print ‘demo’ as the result because it is the last value from the path.
2) basename($Your_path, @Your_suffixes): In this function, it takes path and suffix as the input parameter. The second parameter here is the suffix of the file. Let’s see one sample example to understand the working of the function in more detail how we can pass this using while programming see below;
my $filesample1 = basename("my/sample/path/for/demo.sql", ".sql");
In the above lines of code, we are retrieving the file using the path of the file and suffix for the file. In this way, we will get the file here because the last level for the path is file only so it will return us the file name here.
Points to remember while working with the basename function in Perl:
- This function s used to get the portion of the file name. Not the full file name
- If this function finds the directory name at the end of the path then it will return the directory name only.
- To use this function in Perl we have to include the file library which is part of Perl programming only.
Examples
1) In this example we are trying to fetch the name from the file path using basename function in Perl. It will return us to the last level of the path as the output here. This is also a sample example for beginners to understand usage better.
Example #1
Code:
use File::Basename;
print "Demo to show the basename function in Perl to get the file name or directory portion\n";
my $filesample1 = basename("/my/sample/path/for/demo1");
my $filesample2 = basename("/my/sample/path/for/demo2");
my $filesample3 = basename("/my/sample/path/for/demo3");
my $filesample4 = basename("/my/sample/path/for/demo4");
my $filesample5 = basename("/my/sample/path/for/demo5");
my $filesample6 = basename("/my/sample/path/for/demo6");
print "Here printing the result\n";
print "first path is :: ";
print "$filesample1\n";
print "second is :: ";
print "$filesample2\n";
print "third path is :: ";
print "$filesample3\n";
print "fourth is :: ";
print "$filesample4\n";
print "fivfth path is :: ";
print "$filesample5\n";
Output:
2) In this example we are passing two param inside the basename function one of the path of the file and other one is the suffix for the file. But will going to return the last level form the path. This is also a sample example for beginners to understand usage better.
Example #2
Code:
use File::Basename;
print "Demo to show the basename function in Perl to get the file name or directory portion\n";
print "Here we are using the extension of the file as well passing two param as the input in basename function in Perl\n";
my $filesample1 = basename("https://cdn.educba.com/my/sample/path/for/demo1.txt", ".txt");
my $filesample2 = basename("https://cdn.educba.com/my/sample/path/for/demo2.txt", ".txt");
my $filesample3 = basename("https://cdn.educba.com/my/sample/path/for/demo3.txt", ".txt");
my $filesample4 = basename("https://cdn.educba.com/my/sample/path/for/demo4.txt", ".txt");
my $filesample5 = basename("https://cdn.educba.com/my/sample/path/for/demo5.txt", ".txt");
my $filesample6 = basename("https://cdn.educba.com/my/sample/path/for/demo6.txt", ".txt");
print "Here printing the result\n";
print "first path is :: ";
print "$filesample1\n";
print "second is :: ";
print "$filesample2\n";
print "third path is :: ";
print "$filesample3\n";
print "fourth is :: ";
print "$filesample4\n";
print "fivfth path is :: ";
print "$filesample5\n";
Output:
Conclusion
In this way, we can get the name of the file from the path. We can also pass the parameter as we want there is no restriction for this. It can take the file path as with or without the suffix mentioned during call of the function in Perl.
Recommended Articles
This is a guide to Perl basename. Here we discuss the introduction, How does basename function work in Perl? along with examples respectively. You may also have a look at the following articles to learn more –