Search notes:

Perl function: seek

The following example script reads two lines from itself ($0). Then, it uses seek with a negative argument to go back in the input file by the length of the second line. Thus, next next line that is read from $f is the second line (which will be assigned to $second_line_again).
When the script is run, it will print use warnings;.
#!/usr/bin/perl
use warnings;
use strict;

open (my $f, '<', $0) or die;

my $first_line  = <$f>;
my $second_line = <$f>;


seek ($f, -length($second_line), 1);

my $second_line_again = <$f>;

print $second_line_again;
Github repository about-perl, path: /functions/seek.pl

See also

The libc functions lseek and fseek.
Perl functions

Index