Perl问题:grep之后删除的内容

Perl问题:grep之后删除的内容,perl,Perl,我看到perl grep之后文件的内容被删除了,有什么原因/修复吗?我需要决定grep是否成功 open my $fh, "<", $file or die "can't read open '$file': $OS_ERROR"; $start_sync=0; if (grep{/PATTERN/} <$fh>){ print "word found\n"; $start_sync=1; } else{ print "word not found\n"

我看到perl grep之后文件的内容被删除了,有什么原因/修复吗?我需要决定grep是否成功

open my $fh, "<", $file or die "can't read open '$file': $OS_ERROR";
$start_sync=0;
if (grep{/PATTERN/} <$fh>){
    print "word found\n";
    $start_sync=1;
}
else{
    print "word not found\n";
    $start_sync=0;
}
my @lines = <$fh>;  
close $fh or die "can't read close '$file': $OS_ERROR";  
if($start_sync==1) {edit the same file}
打开我的$fh,“当您调用的第二个参数
时,它会强制菱形操作符上的列表上下文,该操作符读取文件中的所有行。当grep完成时,文件句柄将耗尽并指向

如果要再次读取文件,需要倒带文件句柄位置。用于:

seek $fh, 0, 0;
my @lines = <$fh>;

grep
将处理所有元素,因此即使您已经有了答案,它也会查看每一行。但如果您在十亿行中的第1行找到它,该怎么办?相反,循环直到找到它,然后停止:

while( <$fh> ) {
    next unless /PATTERN/;
    $start_sync = 1;
    last;
    }
while(){
下一步除非/模式/;
$start\u sync=1;
最后;
}

谢谢。我知道这是EOF问题,但因为我不是perl专家,所以我不知道解决方案。感谢您的rootcausing和修复。Works!如果您要阅读所有行,请在grep之前执行,然后grep您的数组
while( <$fh> ) {
    next unless /PATTERN/;
    $start_sync = 1;
    last;
    }