A5下载 - 努力做内容最丰富最安全的下载站!

A5站长下载站

当前位置:A5下载 > 源码技巧 > 父类数据 > perl读写文件代码实例

perl读写文件代码实例

时间:2015-05-08 14:29作者:zhao人气:46

#mode operand create truncate

#read <

#write > yes yes

#append >> yes

Case 1: Throw an exception if you cannot open the file:

use strict;

use warnings;

my $filename = 'data.txt';

open(my $fh, '<:encoding(UTF-8)', $filename)

or die "Could not open file '$filename' with the error $!";

while (my $row = <$fh>) {

chomp $row;

print "$rown";

}

close($fh);

Case 2: Give a warning if you cannot open the file, but keep running:

use strict;

use warnings;

my $filename = 'data.txt';

if (open(my $fh, '<:encoding(UTF-8)', $filename)) {

while (my $row = <$fh>) {

chomp $row;

print "$rown";

}

close($fh);

} else {

warn "Could not open file '$filename' $!";

}

Case 3: Read one file into array

use strict;

use warnings;

my $filename = 'data.txt';

open (FILEIN, "<", $filename)

or die "Could not open file '$filename' with the error $!";

my @FileContents = <FILEIN>;

for my $l (@FileContents){

print "$ln";

}

close FILEIN;

end

标签perl,读写,文件,代码,实例,#mode,operand

相关下载

查看所有评论+

网友评论

网友
您的评论需要经过审核才能显示

公众号