如何在Perl中操作文件指针

Bra*_*don 1 perl file-io file-pointer

因此,我正在阅读日历文件以在文件中插入日期,并且我希望日期保持按时间顺序排列.当我找到日期应该去的地方时,问题出现了,文件已经超过了我要插入的点.

我正在查看的日历文件如下所示:

# November 2010
11/26/2010
11/27/2010
11/28/2010
11/29/2010
11/30/2010
# December
12/24/2010
12/25/2010
12/26/2010
12/27/2010
12/28/2010
12/29/2010
12/30/2010
Run Code Online (Sandbox Code Playgroud)

我的代码看起来像这样:

while (my $line = <FILE>) {
    if (substr($line, 0, 1) =~ m/\#/ || $line =~ m/calendar/) { #if the line is commented out or contains the calendar's name skip to next line
        next;
    }
    chomp($line);
    my ($temp_month, $temp_day, $temp_year) = split(/\//, $line, 3);
    if ($year == $temp_year && $month == $temp_month && $day < $temp_day) {
        ?
    }
}
Run Code Online (Sandbox Code Playgroud)

那么有关于如何指向文件中的前一个位置的任何建议吗?

Dav*_*oss 6

您需要在文件中随机移动的功能是seek.但是有关如何在Perl FAQ中解决这个问题的更多信息 - 如何在文件中更改,删除或插入行,或者附加到文件的开头?