ITeye博客

Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };

standalone · 2012-12-28 16:25 · perl · 编程语言 · Markdown

http://www.perlmonks.org/index.pl?node_id=287647

open FILEHANDLE, 'somefile.txt' or die $!; my $string = do { local $/; };

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 () { $string .= $_; }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。