免费注册 查看新帖 |

Chinaunix

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

File::Find 模块的循环调用 问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-04-05 20:21 |只看该作者 |倒序浏览
本帖最后由 shaunsyb 于 2016-04-22 10:09 编辑
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use File::Find;

  5. my $profile="~/.bash_profile";

  6. open(NEWENV, ". $profile && env|") or die "$!";
  7. while (<NEWENV>){
  8.     if (/(\w+)=(.*)/){
  9.       $ENV{$1}="$2";
  10.     }
  11. }
  12. close NEWENV;

  13. for my $count (0..1) {
  14.         my @all_dir;
  15.         sub wanted {
  16.                 push @all_dir,$File::Find::name if ( $_ =~ /^radius\.log$/ );
  17.         }
  18.         find(\&wanted,"$ENV{radius_home}/radius_log");

  19.         print "@all_dir"."\n";
  20. }

复制代码
使用以上 脚本进行  循环查询的时候,只能打印出第一次 的结果,第二次为空,以下为打印结果:
/radius_log/radius_log/log/radius.log


而使用如下脚本执行时,则可以打印出2次的结果..
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use File::Find;

  5. my $profile="~/.bash_profile";

  6. open(NEWENV, ". $profile && env|") or die "$!";
  7. while (<NEWENV>){
  8.     if (/(\w+)=(.*)/){
  9.       $ENV{$1}="$2";
  10.     }
  11. }
  12. close NEWENV;

  13. my @all_dir;
  14. for my $count (0..1) {
  15.         splice @all_dir;
  16.         sub wanted {
  17.                 push @all_dir,$File::Find::name if ( $_ =~ /^radius\.log$/ );
  18.         }
  19.         find(\&wanted,"$ENV{radius_home}/radius_log");

  20.         print "@all_dir"."\n";
  21. }
复制代码
修改后的脚本就是 将 @all_dir 变量放到了循环之外,实在搞不明白 第一个脚本的问题出在哪里。。。求大神解惑!!

论坛徽章:
0
2 [报告]
发表于 2016-04-07 13:36 |只看该作者
看你用radius, 就知道你肯定是电信的计费系统..

如果只是放在循环体内和循环体外的差别, 显然第二次输出的是第一次的值,因为数组没有被清空...

论坛徽章:
0
3 [报告]
发表于 2016-04-11 11:47 |只看该作者
回复 2# kingfighters


    清空了的    splice @all_dir;
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP