免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2698 | 回复: 12
打印 上一主题 下一主题

小弟刚接触perl,不太熟,麻烦帮忙看下这段代码 [复制链接]

论坛徽章:
1
2015亚冠之阿尔纳斯尔
日期:2015-11-11 18:05:28
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-27 15:39 |只看该作者 |倒序浏览
本帖最后由 kaixin9ok 于 2012-11-27 15:44 编辑

.现在在看小骆驼

我的目的是想遍历一下/etc的文件,判断如果是目录的话,继续往下遍历,这么写只遍历了第一个目录,然后就不往下执行了.
麻烦指导一下,谢谢

#!/usr/bin/perl
use warnings;
use strict;
my $file;
chdir "/etc" or die "cannot chdir to /etc: $!";;
#open FILE,">>/tmp/test.txt" or die "file not exists $!";
my $dir = "/etc";
        opendir DH,$dir or die "Open dir error Directory";
         while ($file = (readdir DH)){
             next if $file =~ m/^\.(\.)?/;
                if( -d $file){
                &readdir_read($file);
                }
                }


sub readdir_read{
        if( -d $_[0]){
                opendir  DH,$_[0] or die "open dir error Director $!";
                while (my $file = readdir DH){
                next if $file =~ m/^\.(\.)?/;
                print $file ."\n";
                }
        }

}

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
2 [报告]
发表于 2012-11-27 15:54 |只看该作者
使用File::Find吧

论坛徽章:
6
卯兔
日期:2013-11-26 14:52:02丑牛
日期:2014-02-19 18:01:25卯兔
日期:2014-05-20 20:34:06白羊座
日期:2014-05-23 13:39:232015亚冠之大阪钢巴
日期:2015-08-07 20:57:582015亚冠之大阪钢巴
日期:2015-09-02 14:09:09
3 [报告]
发表于 2012-11-27 15:56 |只看该作者
  1. $File::Find::dir  = /some/path/
  2.     $_                = foo.ext
  3.     $File::Find::name = /some/path/foo.ext
复制代码

论坛徽章:
1
2015亚冠之阿尔纳斯尔
日期:2015-11-11 18:05:28
4 [报告]
发表于 2012-11-27 15:59 |只看该作者
回复 3# 只是一个红薯



嗯 多谢你的指导


- -我想试下自己写
能帮我看下我写的这个错误点在哪吗?

论坛徽章:
1
2015亚冠之阿尔纳斯尔
日期:2015-11-11 18:05:28
5 [报告]
发表于 2012-11-27 16:00 |只看该作者
回复 3# 只是一个红薯


         就是想知道,遍历目录的整个过程

呵呵

论坛徽章:
0
6 [报告]
发表于 2012-11-27 16:02 |只看该作者
将代码中的DH改成my $dh即可
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. my $file;
  5. chdir "/etc" or die "cannot chdir to /etc: $!";;
  6. #open FILE,">>/tmp/test.txt" or die "file not exists $!";
  7. my $dir = "/etc";
  8. opendir my $dh,$dir or die "Open dir error Directory";
  9. while ($file = (readdir $dh)){
  10.     next if $file =~ m/^\.(\.)?/;
  11.     if( -d $file){
  12.         &readdir_read($file);
  13.     }
  14. }

  15. sub readdir_read{
  16.     if( -d $_[0]){
  17.         opendir  my $dh,$_[0] or die "open dir error Director $!";
  18.         while (my $file = readdir $dh){
  19.             next if $file =~ m/^\.(\.)?/;
  20.             print $file ."\n";
  21.         }
  22.     }
  23. }
复制代码

论坛徽章:
1
2015亚冠之阿尔纳斯尔
日期:2015-11-11 18:05:28
7 [报告]
发表于 2012-11-27 16:08 |只看该作者
回复 6# 首天


    原理何在啊?
为啥用DH就不可以啊?

论坛徽章:
0
8 [报告]
发表于 2012-11-27 16:51 |只看该作者
本帖最后由 xi0ws 于 2012-11-27 17:02 编辑

1. 这个遍历方法有错吧
2. readdir得到的仅仅是文件/文件夹的名字(不包含路径),所以&readdir_read要传递全路径
  1. &readdir_read("$dir\\$file")
复制代码

论坛徽章:
0
9 [报告]
发表于 2012-11-27 16:57 |只看该作者
回复 7# kaixin9ok


    代码只能进入第二层吧

论坛徽章:
0
10 [报告]
发表于 2012-11-27 17:25 |只看该作者
本帖最后由 xi0ws 于 2012-11-28 10:01 编辑
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;

  4. my $dir = "/etc";
  5. readdir_read($dir);

  6. sub readdir_read
  7. {
  8.     return unless -e $_[0];
  9.     opendir my $dh, $_[0] or die "open dir error Director $!";
  10.     while (my $file = readdir $dh){
  11.         next if -f "$_[0]\\$file" || $file =~ m/^\.(\.)?/;
  12.         print $file."\n";
  13.         readdir_read("$_[0]\\$file");
  14.     }
  15.     closedir $dh;
  16. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP