国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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

Original 2017-01-11 13:16:45 473
abstract:這篇文章主要介紹了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


Release Notes

Popular Entries