Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seek function not working in perl

I tried the below code snippet and the seek function doesn't seem to work.

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;
}

Note: All variants of seek doesn't work.

Output:

The position does not change even after the seek. It remains in 613.

Can you guys, check and let me know what is the issue here?

Thanks.

like image 876
Helloworld Avatar asked May 13 '26 17:05

Helloworld


1 Answers

You cannot seek on a pipe.

Either use a temporary file or store the data in memory.

Your choice as to the best solution

like image 53
Ed Heal Avatar answered May 16 '26 07:05

Ed Heal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!