【发布时间】:2014-11-09 07:19:04
【问题描述】:
我正在使用一个 perl 脚本,该脚本将目录名称作为用户的输入并在其中搜索文件。搜索文件后,它会读取文件的内容。如果文件内容包含单词“cricket”,那么使用取消链接功能我应该能够删除该文件。但是使用unlink 执行代码后,目录中仍然存在包含单词“cricket”的文件。请帮忙。我的代码是:
use strict;
use warnings;
use File::Basename;
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 $!";
my $count = 0;
while (my $row = <$fh>) {
chomp $row;
if ($row =~ /cricket/) {
$count++;
}
}
print "$count";
if ($count == 0) {
chomp($filepath);
unlink $filepath;
print " $filepath deleted";
}
}
【问题讨论】:
-
你有写入那个文件的权限吗?
-
看起来您正在尝试
unlink一个打开的文件。
标签: perl