perl 파일 코드 읽기 실례

1477 단어
#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> or die "Could not open file '$filename' with the error $!";
 
while (my $row = ) {
chomp $row;
print "$row
";
}
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> while (my $row = ) {
chomp $row;
print "$row
";
}
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, " or die "Could not open file '$filename' with the error $!";
my @FileContents = ;
for my $l (@FileContents){
print "$l
";
}
close FILEIN;
  
end

좋은 웹페이지 즐겨찾기