本文主要是介绍File::Basename,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在Perl中,如果想将路径和文件名,文件后缀分别提取出来,可以使用perl提供的模块File::Basename来实现。
File::Basename中常用的方法有fileparse, basename, dirname。
fileparse方法会传回包含路经名称三个部份的串列;
dirname方法传回路经位置;
basename方法传回档案名称。
#!/usr/bin/perl -w
use File::Basename;my $full_path_name='/home/zl/perl/basename/base.pl';
my @suffix_list=qw(.pl .txt);my ($name, $path, $suffix)=fileparse($full_path_name, @suffix_list);
print "\$name = $name \n";
print "\$path = $path \n";
print "\$suffix = $suffix \n";my $basename = basename($full_path_name, @suffix_list);
print("\$basename = $basename \n");my $dirname = dirname($full_path_name, @suffix_list);
print("\$dirname = $dirname \n");
结果:
zl@zl-ubuntu:~/perl/basename$ perl basename.pl
$name = base
$path = /home/zl/perl/basename/
$suffix = .pl
$basename = base
$dirname = /home/zl/perl/basename
zl@zl-ubuntu:~/perl/basename$
参考:
语法:http://search.cpan.org/~rjbs/perl-5.18.2/lib/File/Basename.pm
use File::Basename;($name,$path,$suffix) = fileparse($fullname,@suffixlist);$name = fileparse($fullname,@suffixlist);$basename = basename($fullname,@suffixlist);$dirname = dirname($fullname);
http://www.perlcn.com/perlbc/perljj/346.html
这篇关于File::Basename的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!