手机
当前位置:查字典教程网 >脚本专栏 >perl >perl读写文件代码实例
perl读写文件代码实例
摘要:#modeoperandcreatetruncate#readyesyes#append>>yesCase1:Throwanexceptio...

#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读写文件代码实例】相关文章:

perl产生随机数实现代码

使用perl清理电脑上重复文件实现代码(续)

perl哈希的一个实例分析

perl跳过首行读取文件的实现代码

perl从文件中读取数据并输出的实现代码

Perl文件句柄详解

perl处理csv文件的小例子

Perl读写文件简单示例

perl 文件操作总结

perl几个文件操作例子

精品推荐
分类导航