[url]http://www.perlmonks.org/index.pl?node_id=287647[/url]
open FILEHANDLE, 'somefile.txt' or die $!;
my $string = do { local $/; <FILEHANDLE> };
The above idiom is a consise way to "slurp" the entire contents of a file into a scalar without using a loop, such as:
open FILEHANDLE, 'somefile.txt' or die $!;
my $string = '';
while (<FILEHANDLE>) {
$string .= $_;
}
open FILEHANDLE, 'somefile.txt' or die $!;
my $string = do { local $/; <FILEHANDLE> };
The above idiom is a consise way to "slurp" the entire contents of a file into a scalar without using a loop, such as:
open FILEHANDLE, 'somefile.txt' or die $!;
my $string = '';
while (<FILEHANDLE>) {
$string .= $_;
}
本文介绍了一种使用Perl语言高效读取整个文件内容的方法。通过一个简洁的代码示例展示了如何不使用循环就能将文件内容读取到一个变量中。这种方法被称为“slurping”,适用于需要快速读取文件的应用场景。

被折叠的 条评论
为什么被折叠?



