Refreshing Perl String Reader

what happens after eof is achieved using the <> operator in perl?

I read INP1 line by line with

while(<INP1>) {
}

but I need to do this reading several times, and I need to start from the very beginning of the file every time. How can i do this? Is there something like stream updates in perl?

Thanks in advance.

+3
source share
3 answers

If INP1connected to a regular file descriptor (not a socket descriptor or a channel descriptor), you can also seekreturn to the beginning of the file.

while(<INP1>) {
   ...
}
seek INP1, 0, 0;

# do it again
while (<INP1>) {
   ...
}

- , , . , , .

open INP1, '<', $the_file;
@INP1 = <INP1>;
close INP1;

foreach (@INP1) {
   ...
}

# do it again
foreach (@INP1) {
   ...
}
+8

seek, , Tie:: File . , .

+3

You can seekreturn to the beginning:

use Fcntl;
open INP1, ...
while (<INP1>) {
}
seek INP1, 0, SEEK_SET;
while (<INP1>) {
}

This will only work if INP1 is a real file (not a pipe or socket).

+2
source

Source: https://habr.com/ru/post/1776576/


All Articles