1. 一次性将文件中的所有内容读入一个数组中(该方法适合小文件):
open(FILE,"filename")||die"can not open the file: $!";
@filelist=<FILE>;
foreach $eachline (@filelist) {
chomp $eachline;
}
close FILE;
@filelist=<FILE>;
当文件很大时,可能会出现"out of memory"错误。
2. 一次从文件中读取一行,一行行地读取和处理(读取大文件时比较方便):
open(FILE,"filename")||die"can not open the file: $!";
while (defined ($eachline =<FILE>)) {
chomp $eachline;
# do what u want here!
}
close FILE;