Use Perl's File::Basename package to parse full file path information

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.


[plain]  view plain copy
  1. use File::Basename;  
  2. use strict;  
  3. fileparse_set_fstype('MSWin32');  
  4. my $path='c:/perl/bin/perl.exe';  
  5. my($fname, $dir, $ext)=fileparse($path, '.exe');  
  6. print "Location: $ dir / nBasename: $ fname / nExtension: $ ext / n";  
  7. my($basename)=basename($path, '.exe');  
  8. print "basename(): $basename/n";  
  9. my($dirname)=dirname($path);  
  10. 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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326028048&siteId=291194637