[Perl] How to use the Basename module

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.