Search function does not work in perl
I tried the below code snippet and the search function is not working.
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 search options don't work.
Output:
The position does not change even after searching. It remains in 613.
Can you guys check and let me know what the problem is?
Thank.
+3
Helloworld
source
to share
2 answers
You cannot search for a pipe.
Use a temporary file or keep the data in memory.
Your choice of the best solution
+5
Ed heal
source
to share
Try writing the output of your command ls
to a file and opening that file instead of reading the command output directly. You cannot seek
on a transient data stream (like the output of a command), only on data that still exists after reading (like a file).
+1
Dave sherohman
source
to share