如何在 <STDIN> perl 中进行嵌套读取?



我正在编写一个脚本来解析来自Java的线程转储。出于某种原因,当我尝试从子例程或嵌套循环中读取时,它根本不会进入嵌套循环。理想情况下,我希望能够在嵌套循环上对 STDIN 进行操作,否则您将不得不编写一些丑陋的状态转换代码。

在我使用 STDIN 之前,但为了确保我的子例程没有指向 STDIN 的独立指针,我将其打开到$in中。

当我运行它时,它如下所示。您可以看到它永远不会进入嵌套循环,尽管外部循环有更多来自 STDIN 的文件要读取。

~/$ cat catalina.out-20160* | thread.dump.find.all.pl
in is GLOB(0x7f8d440054e8)
found start of thread dump at 2016-06-17 13:38:23 saving to tdump.2016.06.17.13.38.23.txt
in is GLOB(0x7f8d440054e8)
BEFORE NESTED STDIN
BUG!!!!
found start of thread dump at 2016-06-17 13:43:05 saving to tdump.2016.06.17.13.43.05.txt
in is GLOB(0x7f8d440054e8)
BEFORE NESTED STDIN
BUG!!!!
...

代码:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use DateTime::Format::Strptime;
use DateTime::Format::Duration;
use Data::Dumper;
# DO NOT touch ARGV!
Getopt::Long::Configure("pass_through");
# cat catalina.out-* | thread.dump.find.all.pl

sub processThreadDump {
my $in=$_[0];
my $currentLine=$_[1];
my $prevLine=$_[2];
my $parsedDatetime=$_[2];
# 2016-09-28 09:27:34
$parsedDatetime=~ s/[ -:]/./g;
my $outfile="tdump.$parsedDatetime.txt";
print " saving to $outfilen";
print " in is $inn";
open(my $out, '>', $outfile);
print $out "$prevLinen";
print $out "$currentLinen";
print "BEFORE NESTED STDINn";
foreach my $line ( <$in> ) {
print "INSIDE NESTED STDINn";
$line =~ s/R//g; #remove newlines
print $out "$linen";
if( $line =~ m/JNI global references:/ ) {
print "PROPERLY LEFT NESTED STDINn";
close($out);
return;
} elsif( $line =~ m/Found d+ deadlock./ ) {
print "PROPERLY LEFT NESTED STDINn";
close($out);
return;
}
}
print "BUG!!!!n";
close($out);
}
open(my $in, '<-');
print "in is $inn";
my $prevLine;
# read from standard in
foreach my $line ( <$in> ) {
$line =~ s/R//g; #remove newlines
if( $line =~ m/Full thread dump OpenJDK 64-Bit Server VM/ ) {
# we found the start of a thread dump
print "found start of thread dump at ${prevLine}";
processThreadDump($in, $line, $prevLine);
} else {
#print "setting prev line to $linen";
$prevLine=$line;
}
}
close($in);
foreach

遍历列表,因此<>在列表上下文中,因此它会从文件句柄读取所有内容。 因此,当您将$in传递给潜艇时,它没有输入。请参阅 perlop 中的 I/O 运算符。

您可以一次读取一行,while (my $line = <$in>),但我不确定这是否会影响算法的其余部分。

或者,如果您确实提前阅读了所有输入,为什么不只使用一组行。

当你说foreach my $line ( <$in> )时,这会导致perl在开始循环之前读取整个$in文件句柄。 你可能想要的更像是这样的:

while (defined(my $line = <$in>))

这将一次只读取一行,并在您完成时丢弃它。

最新更新