免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Perl 求指点
最近访问板块 发新帖
查看: 1318 | 回复: 5
打印 上一主题 下一主题

求指点 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-02 22:25 |只看该作者 |倒序浏览
#!/usr/bin/perl -W

use strict;

sub lsr_s($) {

    my $cwd = shift;

    my @dirs = ($cwd.'/');


    my ($dir, $file);

    while ($dir = pop(@dirs)) {

        local *DH;

        if (!opendir(DH, $dir)) {

            warn "Cannot opendir $dir: $! $^E";

            next;

        }

        foreach (readdir(DH)) {

            if ($_ eq '.' || $_ eq '..') {

                next;

            }

            $file = $dir.$_;         

            if (!-l $file && -d _) {

                $file .= '/';

                push(@dirs, $file);

            }

            process($file, $dir);

        }

        closedir(DH);

    }

}


my ($size, $dircnt, $filecnt) = (0, 0, 0);


sub process($$) {

    my $file = shift;

    print $file, "\n";

    if (substr($file, length($file)-1, 1) eq '/') {

        $dircnt++;

    }

    else {

        $filecnt++;

        $size += -s $file;

    }

}


lsr_s('.');

print "$filecnt files, $dircnt directory. $size bytes.\n";


不知道哪位大侠可告知 程序中local *DH;
是什么意思 。这个遍历文件的程序为什么会这样写呢?

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
2 [报告]
发表于 2012-11-03 11:08 |只看该作者
local *DH 是过时的动态作用域用法,是为了防止名字冲突,你可以理解为把 DH 之前的值放入一个栈中这个大括号结束后把原来的值恢复。现在直接用 opendir my $dh, 'dir'; 就可以了

作者这里不想用递归就搞了个栈来模拟

论坛徽章:
0
3 [报告]
发表于 2012-11-04 12:53 |只看该作者
非常感谢大侠!!!

论坛徽章:
0
4 [报告]
发表于 2012-11-05 10:14 |只看该作者
sub lsr_s($)
sub process($$)
括号里的$和$$是什么意思啊?

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
5 [报告]
发表于 2012-11-05 10:34 |只看该作者
回复 4# 午夜凋零

$ perldoc perlsub

NAME
       perlsub - Perl subroutines

SYNOPSIS
       To declare subroutines:

           sub NAME;                     # A "forward" declaration.
           sub NAME(PROTO);              #  ditto, but with prototypes
           sub NAME : ATTRS;             #  with attributes
           sub NAME(PROTO) : ATTRS;      #  with attributes and prototypes

           sub NAME BLOCK                # A declaration and a definition.
           sub NAME(PROTO) BLOCK         #  ditto, but with prototypes
           sub NAME : ATTRS BLOCK        #  with attributes
           sub NAME(PROTO) : ATTRS BLOCK #  with prototypes and attributes
           ...
   
   Prototypes
       Perl supports a very limited kind of compile-time argument checking
       using function prototyping.  If you declare

           sub mypush (+@)
           ...

           Declared as                 Called as

           sub mylink ($$)          mylink $old, $new
           sub myvec ($$$)          myvec $var, $offset, 1
           sub myindex ($$;$)       myindex &getstring, "substr"
           sub mysyswrite ($$$;$)   mysyswrite $buf, 0, length($buf) - $off, $of
           ...
   

论坛徽章:
0
6 [报告]
发表于 2012-11-05 15:27 |只看该作者
恩,谢谢各位的指点!!
对于三楼提出的问题,我也在想,可是才学疏浅,也木有想出来啊。
希望有大侠可以帮着看一下吧!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP