老师:您好!
在学习这一课的时候我碰到一些问题想要请教您!
以下是我为测试目录句柄在rhel中创建的目录和文件:
[root@nagios tmp]# pwd
/tmp
[root@nagios tmp]# ls -lF demo/
total 1324
-rw-r--r-- 1 root root 442768 Mar 11 10:07 book1.txt
drwxr-xr-x 2 root root 4096 Mar 11 10:18 shenhua/
-rw-r--r-- 1 root root 442768 Mar 11 10:17 shuyue
-rw-r--r-- 1 root root 442768 Mar 11 10:17 sky.txt
#以上是在tmp目录下创建了待测试的demo目录,在demo目录下有一个子目录shenhua和3个一样的文本文件(三个文本文件有两个是以txt结尾,一个文件名为shuyue)。
以下是我的perl代码:
[root@nagios tmp]# pwd
/tmp
[root@nagios tmp]# cat test.pl
#!/opt/ActivePerl-5.14/bin/perl
use 5.010;
use strict;
use warnings;
my ($dir_to_proccess,$file);
$dir_to_proccess = "/tmp/demo/";
opendir DH,$dir_to_proccess or die "Can't open the dir!";
foreach $file (readdir DH){
next if $file eq "." or $file eq "..";
next if -d $file;
# next unless $file =~ /\.txt$/;
say "$file";
}
closedir DH;
现在的问题是:
1、以上代码运行后得出的结果并不是只显示文件,目录也给显示了,以下是结果:
[root@nagios tmp]# ./test.pl
shenhua
shuyue
sky.txt
book1.txt
2、同样将next if -d $file 改为 next if -f $file;结果也不会只显示目录,以下为结果:
[root@nagios tmp]# ./test.pl
shenhua
shuyue
sky.txt
我想了很久换成while试过了结果也是一样,想不明白,请老师帮忙看下以上两个疑问,谢谢!
help i cheated on my boyfriend
go how to cheat on boyfriend