The file path can be divided into: location, file, extension. Take c:/perl/bin/perl.exe for example, the location is c:/perl/bin, the file name is perl, and the extension is .exe.
By default, File::Basename assumes you're using a Unix-style pathname, but the file specification can be changed by calling fileparse_set_fstype. Available parameters include VMS, MSDOS, MacOS, AmigaOS, MSWin32.
Commonly used methods in File::Basename are fileparse, basename, dirname.
The fileparse method returns a list containing the three parts of the path name.
The dirname method returns the path location.
The basename method returns the file name.
- use File::Basename;
- use strict;
- fileparse_set_fstype('MSWin32');
- my $path='c:/perl/bin/perl.exe';
- my($fname, $dir, $ext)=fileparse($path, '.exe');
- print "Location: $ dir / nBasename: $ fname / nExtension: $ ext / n";
- my($basename)=basename($path, '.exe');
- print "basename(): $basename/n";
- my($dirname)=dirname($path);
- print "dirname(): $dirname/n";
Output result:
Location: c:/perl/bin/
Basename: perl
Extension: .exe
basename(): perl
dirname(): c:/perl/bin
The second parameter (optional parameter) of fileparse and basename is general A list of regular expressions, which can be used to remove extensions, allowing you to compare certain special files.