1,判断文件是否为空
没找到perl下直接判断文件为空的好方法,突然发现这个 Tie::File 模块挺有用,代码如下:
#!/usr/bin/perl -w
use strict;
use Tie::File;
my $filename = '/opt/test/aa';
tie my @array, 'Tie::File', $filename or die "$!";
if ( @array ) {
print "$filename is have file. \n";
}
else {
print "$filename is empty. \n";
}
2,判断目录是否为空
判断目录为空的方法,无非也是查看目录下是否有文件,注意也可以查看是否有隐藏文件,带#那行代码就可以。
#!/usr/bin/perl -w
use strict;
my $dirname = "/opt/null";
my @dir_files = <$dirname/*>;
#my @dir_files = <$dirname/* $dirname/.*>;
if ( @dir_files ) {
print "$dirname is have file. \n";
foreach my $file (@dir_files) {
print "$file\n";
}
}
else {
print "$dirname is empty. \n";
}
阅读(10276) | 评论(1) | 转发(1) |