问下 perl 怎么读取整个文件里的内容(包括所有换行)?

2016 年 10 月 31 日
 darkbread
比如我有一个 data.txt ,内容里有若干换行,比如:
para1

para2

para3


我想把整个文本保存到变量里,可是我用 open 时,一遇到换行就返回了,只能得到 para1 。
3752 次点击
所在节点    问与答
6 条回复
shierji
2016 年 10 月 31 日
xss
2016 年 10 月 31 日
open F,'<','./lsmod.log' or die('open failed');
@content = <F>;
cfy
2016 年 10 月 31 日
``` Perl
use File::Slurp qw/read_file/;

my @contents = read_file($file_path);
```
ltux
2016 年 10 月 31 日
Perl 官方文档
Perl FAQ: How can I read in an entire file all at once?
http://perldoc.perl.org/perlfaq5.html#How-do-I-print-to-more-than-one-file-at-once%3f
ltux
2016 年 10 月 31 日
twl007
2016 年 11 月 1 日
while (<>) {
push(@content, $_);
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/316771

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX