foreach循环中未初始化的错误

时间:2015-05-06 15:29:33

标签: perl

尝试使用https://grepular.com/projects/IMAPExpire

$ ./imapexpire.pl --test --user user --passfile ~/imapexpire.pass --folders exceptions --age 100 --host myserver
TEST  : You're running in test mode, so the deletions wont actually take place
ACTION: Delete mail which arrived before 27-Jan-2015 from: exceptions
Use of uninitialized value $line in substitution (s///) at /usr/local/share/perl5/IMAP/Client.pm line 560, <GEN0> line 14.

Client.pm:555:

sub parse_search (@) {
    my (@resp) = @_;
    my @results = ();
    # find SEARCH line and process results
    foreach my $line (@resp) {
        next unless ($line =~ s/^\*\s+SEARCH\s+([\d+\s]+)\s*\r\n$/$1/);
                @results = split(/ /,$line);
                last; # theres only 1 line
    }
    return(wantarray ? @results : @results ? sequencify(@results) : undef );
}

如果$line未初始化,为什么foreach会重复?

我在RHEL 6.5上。跑yum install perl-IO-Socket-SSLcpan IMAP::Client

1 个答案:

答案 0 :(得分:2)

因为数组可以包含undef

my @list_of_things = ( "fish", undef, "carrot" ); 

foreach my $line ( @list_of_things ) { 
   print $line;
}

你可能通过undef注入了@resp,但是没有看到那里发生了什么,我不知道它是从哪里来的。

解决方法可能是:

next unless defined $line;