1、读取某文件,如果该文件不存在,则报错,并提示出错原因
open (DB, "/home/ellie/myfile") or die "Can't open file: $!\n";
open(FH, "<filename"); # Opens "filename" for reading.读
# The <; symbol is optional.
open(FH, ">filename"); # Opens "filename" for writing.写
# Creates or truncates file.
open(FH, ">>filename"); # Opens "filename" for appending.追加
# Creates or appends to file.
open(FH, "+<filename"); # Opens "filename" for read, then write.写读后写
open(FH, "+>filename"); # Opens "filename" for write, then read.先写后读
close(FH);
#!/usr/bin/perl open(FH, "<d:/readtest.txt") or die "Can't open file: $!\n"; while(<FH>){ print }
4、文件属性
#!/usr/bin/perl
my $file="d:/readtest.txt";
# Is it readble, writeable, and executable?
print "File is readable, writeable, and executable\n" if -r $file and -w _ and -x _;
# When was it last modified?
print "File was last modified ",-M $file, " days ago.\n";
#若为目录则打印
print "File is a directory.\n " if -d $file; # Is it a directory?
File is readable, writeable, and executable
File was last modified 0.0251851851851852 days ago.
-w _为-w $file的简写。