Categorized | Biocompute

Perl:用File::Basename来获取文件名

Posted on 31 七月 2009 by 柳城 ,阅读 1,579

以下内容来自Code Snippets。PerlFile::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:用File::Basename来获取文件名 | 柳城

赞助商

Leave a Reply

广告招租

[强] [握手] [可爱] [ok] [呲牙] :) [偷笑] [流泪] [疑问] [亲亲] [擦汗] [得意] [衰] [可怜] [抱拳] [坏笑] more »

无觅相关文章插件,快速提升流量