手机
当前位置:查字典教程网 >脚本专栏 >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批量查询ip归属地的方法代码

Perl从文件中读取字符串的两种实现方法

perl数组的多数字下标示例代码

perl实现blog备份的脚本代码

perl面向对象实例

perl产生随机数实现代码

Perl合并文本的一段实例代码

Perl生成纯HTML代码二维码实例

精品推荐
分类导航