最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

perl讀寫(xiě)文件代碼實(shí)例

 更新時(shí)間:2015年05月08日 11:21:11   投稿:junjie  
這篇文章主要介紹了perl讀寫(xiě)文件代碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下

#mode operand create truncate
#read < 
#write >  yes yes 
#append >> yes

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

復(fù)制代碼 代碼如下:

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 "$row\n";
}
close($fh);
  

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

復(fù)制代碼 代碼如下:

use strict;
use warnings;
 
my $filename = 'data.txt';
if (open(my $fh, '<:encoding(UTF-8)', $filename)) {
while (my $row = <$fh>) {
chomp $row;
print "$row\n";
}
close($fh);
} else {
warn "Could not open file '$filename' $!";
}
  

Case 3: Read one file into array

復(fù)制代碼 代碼如下:

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 "$l\n";
}
close FILEIN;
  

end

相關(guān)文章

最新評(píng)論

上犹县| 建宁县| 巴彦县| 三门县| 桐梓县| 阿合奇县| 泾川县| 汝阳县| 夏邑县| 屏东县| 泰来县| 团风县| 仙桃市| 阿合奇县| 济阳县| 鹿邑县| 海南省| 许昌县| 和龙市| 友谊县| 泾源县| 观塘区| 蓬莱市| 麻栗坡县| 兴业县| 郁南县| 西乡县| 庆阳市| 东乌| 内丘县| 芷江| 枣强县| 北京市| 定西市| 鲁甸县| 涟源市| 丹阳市| 通州区| 腾冲县| 奉节县| 建平县|