Perl Language Rewind a filehandle


Sometimes it is needful to backtrack after reading.

# identify current position in file, in case the first line isn't a comment
my $current_pos = tell; 

while (my $line = readline $fh)
    if ($line =~ /$START_OF_COMMENT_LINE/)
        push @names, get_name_from_comment($line);
    else {
        last; # break out of the while loop
    $current_pos = tell; # keep track of current position, in case we need to rewind the next line read

# Step back a line so that it can be processed later as the first data line
seek $fh, $current_pos, 0;