【问题标题】:Seek function not working in perl搜索功能在 perl 中不起作用
【发布时间】:2015-02-27 10:17:52
【问题描述】:

我尝试了下面的代码sn-p,seek函数似乎不起作用。

funct("ls -ltr /scratch/dummy/dum*");

sub funct {

print "\nRecording\n";
open(SENSOR_LIST1, "$_[0] |") || die "Failed to read sensor list: $!\n";
for $sensor_line1 (<SENSOR_LIST1>) {
   print "$sensor_line1";
}
my $pos = tell SENSOR_LIST1;
print "\nposition is $pos"; #Here the position is 613

print "\nRecording again";
seek (SENSOR_LIST1, SEEK_SET, 0);
$pos = tell SENSOR_LIST1; # Here again the position is 613, even after a seek
print "\nposition now is $pos";

for $sensor_line1 (<SENSOR_LIST1>) {
        print "$sensor_line1";
    }
close SENSOR_LIST1;
}

注意:所有的 seek 变体都不起作用。

输出:

即使在搜索之后位置也不会改变。它仍然在 613 中。

你们能检查一下并告诉我这里有什么问题吗?

谢谢。

【问题讨论】:

    标签: perl file-handling


    【解决方案1】:

    你不能在管道上寻找。

    要么使用临时文件,要么将数据存储在内存中。

    您对最佳解决方案的选择

    【讨论】:

      【解决方案2】:

      尝试将ls 命令的输出写入文件并打开该文件,而不是直接读取命令的输出。您不能seek 用于瞬态数据流(例如命令的输出),只能用于读取后仍然存在的数据(例如文件)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-28
        • 1970-01-01
        • 2022-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-07
        • 1970-01-01
        相关资源
        最近更新 更多