抛出异常信息时,带\n则不输出更多信息,否则要有at filename line XXX。
练习(f61.pl):
$ cat f61.pl
#!/usr/bin/perl -w
open(F51,"XXXX.XX") or warn "Can not open file: $!";
open(F51,"XXXX,XX") or warn "Can not open file: $!\n";
open(F51,"abc.txt") or die "Can not open file: $!";
$contents = ;
#chomp $contents;
print qq(\n\$contents=$contents);
@contents = ;
#chomp @contents;
print qq(\n\@contents=@contents);
close(F51);
open(F51,"abc.txt") or die "Can not open file: $!";
print("\n use operator read lines: \n");
while(){
print(qq($_));
}
close(F51);
$ perl f61.pl
Can not open file: No such file or directory at f61.pl line 2.
Can not open file: No such file or directory
$contents=first line
@contents=second line
third line
use operator read lines:
first line
second line
third line
写文件和打开文件语法相似,不同的是路径前加>表示打开时先清空文件,>>表示从文件末尾添加。
练习(f62.pl):
#!/usr/bin/perl -w
open(F51,">efg.txt") or die $!;
print F51 "line 1\n";
print F51 "line 2\n";
print F51 "line 3";
close(F51);
print("\nshow file:\n");
system("cat efg.txt");
open(F51,">>efg.txt") or die $!;
print F51 "\nline 4";
close(F51);
print("\nshow file:\n");
system("cat efg.txt");
print("\n");
$ perl f62.pl
show file:
line 1
line 2
line 3
show file:
line 1
line 2
line 3
line 4
当你将数据写入一个文本文件时, P e r l将\ n字符序列转换成你的操作系统使用的记录分隔符。在U N I X中,\ n变成一个ASCII 10(L F);在M a c i n t o s h上,\ n转换成ASCII 13(C R);在D O S和Wi n d o w s系统上,它变成序列ASCII 13和ASCII 10(C R L F)。
每当你真的想要写入二进制数据并且不希望P e r l或操作系统对它进行转换时,你必须使用b i n m o d e函数,将文件句柄标记为二进制文件。应该在文件句柄打开之后和对它进行输入输出之前使用b i n m o d e。
open(F51,"abc.gif") or die "$!";
binmode(F51);
#...
print F51 "...";
#...
close(F51);
对文件进行检查:
-r File is readable by effective uid/gid.
-w File is writable by effective uid/gid.
-x File is executable by effective uid/gid.
-o File is owned by effective uid.
-R File is readable by real uid/gid.
-W File is writable by real uid/gid.
-X File is executable by real uid/gid.
-O File is owned by real uid.
-e File exists.
-z File has zero>
-s File has nonzero> -f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO), or Filehandle is a pipe.
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.
-u File has setuid bit set.
-g File has setgid bit set.
-T File is an ASCII text file (heuristic guess).
-B File is a "binary" file (opposite of -T).
-M Script start time minus file modification time, in days.
-A Same for access time.
-C Same for inode change time (Unix, may differ for other platforms)
Example:
while () {