我有一个程序,它将目录名称作为用户的输入,并搜索目录内的所有文件并打印文件的内容。有什么办法可以读取文件的扩展名并读取指定扩展名的文件的内容吗?例如,它应该读取“.txt”格式的文件的内容。Perl找到文件的扩展名
我的代码是
use strict;
use warnings;
use File::Basename;
#usr/bin/perl
print "enter a directory name\n";
my $dir = <>;
print "you have entered $dir \n";
chomp($dir);
opendir DIR, $dir or die "cannot open directory $!";
while (my $file = readdir(DIR)) {
next if ($file =~ m/^\./);
my $filepath = "${dir}${file}";
print "$filepath\n";
print " $file \n";
open(my $fh, '<', $filepath) or die "unable to open the $file $!";
while (my $row = <$fh>) {
chomp $row;
print "$row\n";
}
}
您是否在寻找标准的核心Perl模块[File :: Basename](http://perldoc.perl.org/File/Basename.html)? – 2014-11-09 05:51:13
实际上,当我打开文件时,它包含文件名列表。我必须检查是否有.txt文件存在与否,这是我的要求 – user2775241 2014-11-09 06:04:09
行;以及File :: Basename提供的哪些设施可以帮助您?什么是难以理解的手册页? – 2014-11-09 06:06:27