perl 文件操作
http://blog.csdn.net/xrt95050/archive/2011/05/03/6386662.aspxperl 文件操作 收藏
打开、关闭文件
open (filevar, filename)
filevar为文件句柄,或者说是程序中用来代表某文件的代号
filename为文件名,其路径可为相对路径,亦可为绝对路径
open(FILE1,"file1");
open(FILE1, "/u/jqpublic/file1");
打开文件时必须决定访问模式
open(FILE1,"file1");
read
open(outfile,">outfile");
write写模式将原文件覆盖,原有内容丢失
open(appendfile, ">>appendfile");
append
open的返回值用来确定打开文件的操作是否成功,成功时返回非零值,失败时返回零:
if (! open(MYFILE, "myfile")) {
die ("cannot open input file file1\n");
}
open (MYFILE, "file1") || die ("Could not open file");
close (MYFILE);
例子.读文件并显示
#!/usr/bin/perl
&gotest("/home/macg/perltest/gogo");
&gotest("/home/macg/www/index.html");
&gotest("jk");
sub gotest{
my(@tmp)=@_;
open (MYFILE, $tmp) || die ("Could not open file");
@array = ; 此句不是读一行,而是读整个文件
foreach (@array) {
print $_;
}
close(MYFILE);
}
$ ./tip.pl
kkkkk 第一个文件gogo读出
第二个文件index.html读出
第三个文件jk不存在,程序走die语句
Could not open file at ./tip.pl line 9
打开管道文件--------操作非常简单,就是以带管道符号的命令作为文件名字符串
执行一个管道命令
假设管道命令创建一个临时文件
再OPEN这个临时文件到句柄
$ vi tip.pl
#!/usr/bin/perl
&gotest("ls -l |");
sub gotest{
my(@tmp)=@_;
open (MYFILE, $tmp) || die ("Could not open file");
@array = ;
foreach (@array) {
print $_;
}
close(MYFILE);
}
$ ./tip.pl
total 16
-rw-rw-r--1 macg macg 6 Mar 16 13:06 gogo
-rwxrwxr-x1 macg macg 192 Mar 17 16:53 tip.pl
读文件
$line = ;
读一行
并把文件指针向后移动一行
@array = ;
读全部
文件的每一行(含回车符)为@array的一个字符串元素
最简单的显示文件
@array = ; 一次读整个文件,读入一个字符串数组
foreach (@array) { 再打印字符串数组的每一个元素(每一行)
print $_;
}
$ ./tip.pl
my($line);
while ($line=) {循环读一行,读不出就为NULL(0)
print $line;
}
$line = ; 从键盘读一行
,类似C的gets();
chomp 函数,截去变量尾端的\n换行,常与键盘输入合用,方法有二種:
1 $yourans=;
chomp $yourans;
2 chomp ($yourans=);
注意:一定不要用while (chomp($line=)),因为chomp总是返回0值,和while($line=)是不同的
while($line=){
chomp($line);把 chomp放在循环里边
…
}
和的区别
先说相同点:都支持标准输入读取
不同点: 可以将输入定向到命令行参数
vireadfile.pl
#! /usr/bin/perl
while () {
print;
}
./readfile.pl index.html 就是读取第一个命令行参数所指的文件
./readfile.pl 如果不带任何参数执行,就是由标准输入STDIN读取
写文件 print/printf句柄 (字串);
open(OUTFILE, ">outfile");
print OUTFILE ("Here is an output line.\n");
print STDERR ("File file1 exists.\n");
print STDOUT ("File file1 exists.\n");
最简单的文件COPY
#!/usr/bin/perl
&gotest("ls -l |","test");
sub gotest{
my(@tmp)=@_;
open (READFILE, $tmp) || die ("Could not open file");
open (WRITEFILE, ">".$tmp) || die ("Could not open file");
my($line);
while ($line=) {
print WRITEFILE $line;
}
close(READFILE);
close(WRITEFILE);
}
$ ./tip.pl
$ ls
gogotesttip.pl
$ cat test
-rw-rw-r--1 macg macg 6 Mar 16 13:06 gogo
-rwxrwxr-x1 macg macg 297 Mar 17 17:43 tip.pl
上例同时也是一次管道文件的建立,相当于ls –l >test
-e文件是否存在 -d 目录是否存在
#!/usr/bin/perl
chomp($file=);
&gotest($file);
sub gotest{
my(@tmp)=@_;
if (-e $tmp) {
print "file is exist\n";
} else { print "file not found\n"; }
}
$ ./tip.pl
gogo
file is exist
$ ./tip.pl
kd
file not found
#!/usr/bin/perl
chomp($file=);
&gotest($file);
sub gotest{
my(@tmp)=@_;
if (-d $tmp) {
print "directory is exist\n";
} else { print "directory not found\n"; }
}
$ ls -F
gogotesttestdir/tip.pl*
$ ./tip.pl
kj
directory not found
$ ./tip.pl
testdir
directory is exist
if (!-e $file) 如果文件不存在
-r,-w,-x 权限
if (-w $file) {
print "$file 写权限!\n";
}
if (-x $file) {
print "$file 读权限!\n";
}
-z是否为空文件,-s是否非空
if (-z $tmp) {
print "file is empty\n";
}
if ($len= -s $tmp) {-s不仅能判断文件非空,还兼有计算文件大小的工作
print "file length is $len \n";
}
$ touch pp
$ ./tip.pl
pp
file is empty
$ ./tip.pl
gogo
file length is 6
-l是否为符号链接
-T是否为文本文件
基本文件操作
删文件
#!/usr/bin/perl
chomp($file=);
&gotest($file);
sub gotest{
my(@tmp)=@_;
my($len);
unlink $tmp if -e $tmp;
}
$ ls
gotesttestdirtip.pl
$ ./tip.pl
go
$ ls
testtestdirtip.pl
$ ls
ddtesttestdirtip.pl
$ ./tip.pl
/home/macg/perltest/dd 全路径删除
$ ls
testtestdirtip.pl
rename("原文件名", "新名");
#!/usr/bin/perl
&gotest("gogo","dd");
sub gotest{
my(@tmp)=@_;
rename($tmp,$tmp);
}
$ ls
gogopptesttestdirtip.pl
$ ./tip.pl
$ ls
ddpptesttestdirtip.pl
取文件属性,共13个属性
#!/usr/bin/perl
chomp($file=);
&gotest($file);
sub gotest{
my(@tmp)=@_;
my(@sta)=stat($tmp);
my($j);
for($j=0;$j
页:
[1]