Perl:用File::Basename来获取文件名
以下内容来自Code Snippets。Perl的File::Basename 模块
File::Basename 模块被用来析取文件路径中的目录,文件名以及后缀。我的这个简单例子演示了如何获取文件后缀。
至于详细用法,用perldoc File::Basename命令查看。
DESCRIPTION
These routines allow you to parse file specifications into
useful pieces using the syntax of different operating sys-
tems.
#!/usr/bin/perl
use strict;
use File::Basename;
use CGI qw/:standard/;
#if running as a CGI application
#use CGI::Carp qw/fatalsToBrowser/;
# uncomment for debugging onlyprint header,start_html;
# if running as a CGI application
my $dir = '/home/username/public_html';
opendir(DIRHANDLE,$dir) or die "Can't open $dir: $!";
my @filenames = sort readdir(DIRHANDLE);
close(DIRHANDLE);
foreach my $file (@filenames) {
my(undef, undef, $ftype) = fileparse($file,qr"\..*");
print "$ftype
\n";
}
print end_html; #if running as a CGI application
有点相关的文章
- 如何用perl处理测序文件 (1.000)
- perl常用的内置特殊变量 (1.000)
- perl:使用system函数 (1.000)
- 用Perl下载NCBI的Blast库(Blastdb) (1.000)
- 用Perl创建UTF-8的文件 (1.000)
- PHP中判断一个数组是否为空? (RANDOM - 0.500)