且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Perl中的大文件读取特定行

更新时间:2023-08-26 18:22:52

在继续之前,了解fork的工作原理很重要.当您fork一个进程时,操作系统使用写时复制语义共享大量父进程和子进程的内存;只需分开分配父级和子级之间的内存量即可.

Before you go further, it's important to understand how fork works. When you fork a process, the OS uses copy-on-write semantics to share the bulk of the parent and child processes' memory; only the amount of memory that differs between the parent and child need to be separately allocated.

要在Perl中读取文件的一行,这是一种简单的方法:

For reading a single line of a file in Perl, here's a simple way:

open my $fh, '<', $filePath or die "$filePath: $!";
my $line;
while( <$fh> ) {
    if( $. == $lineWanted ) { 
        $line = $_;
        last;
    }
}

这使用特殊的$.变量,该变量保存当前文件句柄的行号.

This uses the special $. variable which holds the line number of the current filehandle.