需要有关perl程序的帮助

| 好的,所以我尝试进行哈希处理,如果数组中的任何字符串包含哈希中的键(而不是实际键名),则将其丢弃。否则打印出字符串。这个问题与findHidden子例程的一部分有关。我尝试了很多不同的方法,下面将对有问题的地方进行评论。我确定有人有答案,总是在堆栈溢出时得到答案:)
#!/usr/bin/perl
# Configure
use strict;
use warnings;
use Data::Dumper;
#
sub findHidden;
sub GetInfo; 
sub defineHash;
##############

$passwd = \'/etc/passwd\';
%info = ();

sub GetInfo {
        die \"Cannot open: $passwd\"
        unless (open(PW,$passwd));
        while(<PW>) {
                chomp;
                my ($uname,$junk1,$junk2,$junk3,$domain,$home) = split(\':\', $_);
                next unless ($home =~ /vs/);
                %info = (
                        domain  => $domain,
                        home    => \"$home/\",
                        tmp     => \"$home/tmp\",
                        htdocs  => \"$home/www/htdocs\",
                        cgibin  => \"$home/www/cgi\\-bin\",
                );
                print \"\\n\" . $info{domain} . \"\\n\";
                print \"+\"x40,\"\\n\\n\";
                findHidden($info{tmp});
        }
}
sub findHidden {
        defineHash;
        print \"Searching \" . $_[0] . \"\\n\";
        print \"-\"x30,\"\\n\\n\";
        @hidden = `find $_[0] -iname \".*\"`;
        for(@hidden) {
                foreach $key (keys % hExcludes) {
                        if ($_ =~ /$key/){    #
                                last;         # This portion is 
                        }else{                # Only an issue when using more
                                print \"$_\";   # than 2 keys in my hash.
                                last;
                        }
                }
        }
}
sub defineHash {
        %hExcludes = ();
        %hExcludes = map { $_, 1 } (
                \'spamd\',\'.nfs\'     # If I add another key here, it breaks.
        );

        %knownExploits = 
                ( );
        print Dumper \\%hExcludes;
}
GetInfo;
这可行,并打印出如下内容: /somedir/tmp/.testthis /somedir/tmp/.sdkfbsdif /somedir/tmp/.asdasdasd 我了解为什么它不起作用,因为它遍历某些错误和某些肯定的密钥,我只是想不出如何使它执行我想要的操作,请假设我可能想要10个密钥。我知道有一些方法可以不使用哈希键值作为排除对象,但这是我想要完成的。 我也尝试了如下所示将shift @hidden转换为无效。
                foreach $key (keys % hExcludes) {
                        if ($_ =~ /$key/){    #
                                last;         #
                                shift @hidden;# This portion is 
                        }else{                # Only an issue when using more
                                print \"$_\";   # than 2 keys in my hash.
                                last;
                        }
另外,请记住,只有在我添加第三个...或更多键时,事情才会停止。
        %hExcludes = map { $_, 1 } (
                \'spamd\',\'.nfs\',\'key3\'     # If I add another key here, it breaks
        );
    
已邀请:
您需要的是:
    @hidden = `find $_[0] -iname \".*\"`;
    for(@hidden) {
        undef $isExcluded;
        foreach $key (keys % hExcludes) {
            if ($_ =~ /$key/){
                $isExcluded=1;
                last;
            }
        }
        if( ! $isExcluded ) {
            print \"$_\";
        }
    }
无论通过hExcludes的键进行扫描时发生了什么,代码在第一个键上都会遇到
last
,并且不再处理。您需要设置一个标志并继续进行迭代,直到没有更多可设置的键或找到匹配项为止。然后,您可以打印出不匹配的值。     

要回复问题请先登录注册