亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

原創(chuàng) 2017-01-11 13:16:45 473
摘要:這篇文章主要介紹了perl讀寫文件代碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼.#mode operand create truncate#read < #write >  yes yes #append >> yesCase 1: Throw an exception if you cannot open the file:u

這篇文章主要介紹了perl讀寫文件代碼實(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:

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:

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

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


發(fā)布手記

熱門詞條