open FH, '<', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for writing. Creates the
#file_name if it doesn't already exist
#and will delete/overwrite a pre-existing file of the same name
open FH, '>', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for appending. Creates the
#file_name if it doesn't already exist
open FH, '>>', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for a 'read/write'.
#Will not create the file if it doesn't
#already exist and will not delete/overwrite
#a pre-existing file of the same name
open FH, '+<', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for a 'read/write'. Will create
#the file if it doesn't already exist and will
#delete/overwrite a pre-existing file
#of the same name
open FH, '+>', "$file_name.txt" or die "Error:$!\n";
#Open the 'txt' file for a 'read/append'. Will create
#the file if it doesn't already exist and will
#not delete/overwrite a pre-existing file
#of the same name
open FH, '+>>', "$file_name.txt" or die "Error:$!\n"; 删除文件
use strict ;
use warnings ;
sub test{
open FILE, '<', "d:/code/test.txt" or die $! ;
my $olds = $/ ;
$/ = undef ;
my $slurp = <FILE> ;
print $slurp, "\n" ;
$/ = $olds ;
close FILE;
}
&test() ;
1
也可以使用local关键字来将$/设置为局部变量,这样跳出作用域后,$/又恢复了原来的值。
use strict ;
use warnings ;
sub test{
local $/ ; #??? local $/ = undef ;
open FILE, '<', "d:/code/zdd.txt" or die $! ;
my $slurp = <FILE> ;
print $slurp, "\n" ;
}
&test() ;
1;
最好的方法是使用模块,这样比自己写安全,File::Slurp、IO::All都可以的。