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