Question: How do I separate the path, file name, and file suffix of a file?
Solution: Perl-file:basename Module
Description: In Perl scripts, three commonly used functions can be called after using the Use File::basename module: Fileparse, basename, dirname.
1. Fileparse: Enter parameter full path name and suffix list, return three values: file name, path, suffix, and store in list.
2. basename: Enter parameter full path name and suffix list; Return file name
3. DirName: Input parameter full path name; Return file path
Example:
1 #!/usr/bin/perl2 3 UseStrict;4 Usewarnings;5 UseFile::Basename;6 7 my $fullname='/home/qilzhao/perl_basename.pl';8 my @suffixlist=QW (. pl. txt. sv.v);9 my($name,$path,$suffix) =fileparse ($fullname,@suffixlist);Ten One Print "name= $name \ n"; A Print "path= $path \ n"; - Print "suffix= $suffix \ n"; - the $name=fileparse ($fullname,@suffixlist); - Print "name= $name \ n"; - - my $Basename=basename ($fullname,@suffixlist); + Print "basename= $Basename \ n"; - + my $dirname= DirName ($fullname); A Print "dirname= $dirname \ n";
Execute the above script to get the following result:
Name=perl_basenamepath=/home/qilzhao/suffix=.plname=perl_basenamebasename=perl_basenamedirname=/home/qilzhao
[Perl] How to use the Basename module