最近在学习perl脚本语言,记录一下两种perl语言文本读写的方式:
第一种:
#! /user/bin/perl -w
my $in_file = "/目录/input.txt/";
my $out_file = "/目录/output.txt/";
sub main(){
open(FILE_IN, $in_file) || die "can not open $in_file"; #read
#open(FILE_IN, "<$in_file") || die "can not open $in_file"; #read,same as previous row
open(FILE_OUT,">$out_file") || die "can not open $out_file"; #write
#open(FILE_OUT,">>$out_file") || die "can not open $out_file"; #append
while(<FILE_IN>){
chomp();
print(FILE_OUT $_,"\n");
}
close(FILE_IN);
close(FILE_OUT);
}
&main();
第二种:引用IO:File模块
#! /user/bin/perl -w use IO::File; my $in_file = "/目录/input.txt/"; my $out_file = &