佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > perl讀寫文件代碼實例

perl讀寫文件代碼實例

熱門標(biāo)簽:百度AI接口 硅谷的囚徒呼叫中心 語音系統(tǒng) 電話運營中心 客戶服務(wù) 企業(yè)做大做強 Win7旗艦版 呼叫中心市場需求

#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

標(biāo)簽:山西 安康 海南 喀什 長沙 濟南 崇左 山西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《perl讀寫文件代碼實例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    逊克县| 石狮市| 德惠市| 濮阳市| 凤凰县| 苏尼特左旗| 石台县| 温州市| 灵武市| 那曲县| 磴口县| 仪征市| 吉首市| 旺苍县| 宁陕县| 区。| 乳源| 大荔县| 工布江达县| 峨山| 宣汉县| 烟台市| 屏南县| 青田县| 南安市| 鸡泽县| 晴隆县| 靖宇县| 吉林省| 环江| 浦城县| 朝阳区| 云梦县| 称多县| 双桥区| 康保县| 逊克县| 乐清市| 松溪县| 绥中县| 翼城县|