perl文件操作总结(全面讲解perl的文件操作)
perl 文件操作,包括打开、关闭文件,读取、定入文件等。 原文链接:http://www.jbxue.com/article/3153.html open(FILE1,"file1"); "/u/jqpublic/file1"); 打开文件时必须决定访问模式 read
open(outfile,">outfile");
write写模式将原文件覆盖,原有内容丢失 open(appendfile,">>appendfile");
append
open的返回值用来确定打开文件的操作是否成功,成功时返回非零值,失败时返回零: if (! open(MYFILE,"myfile")) {
die ("cannot open input file file1n");
}
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)=@_;
$tmp[0]) || die ("Could not open file");
@array = <MYFILE>; 此句不是读一行,而是读整个文件
foreach (@array) {
print $_;
}
close(MYFILE);
}
[macg@localhost perltest]$ ./tip.pl
kkkkk 第一个文件gogo读出 第二个文件index.html读出 <frameset rows=20%,*> 第三个文件jk不存在,程序走die语句 [macg@localhost perltest]$ vi tip.pl !/usr/bin/perl &gotest("ls -l |"); @array = <MYFILE>; close(MYFILE); } [macg@localhost perltest]$ ./tip.pl total 16 -rw-rw-r-- 1 macg macg 6 Mar 16 13:06 gogo -rwxrwxr-x 192 Mar 17 16:53 tip.pl 读文件 $line = <MYFILE>; foreach (@array) { 再打印字符串数组的每一个元素(每一行) @localhost perltest]$ ./tip.pl
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<frameset rows=20%,*>
<frame src="title.html" frameborder="no" scrolling="no">
<frameset cols=30%,*>
</frameset>
</frameset>
$line);
while ($line=<MYFILE>) {循环读一行,读不出就为NULL(0)
$line;
}
$line =<STDIN> ; 从键盘读一行,类似C的gets();
chomp 函数,截去变量尾端的n换行,常与键盘输入合用,方法有二種: 1)$yourans=<STDIN>; 2)chomp ($yourans=<STDIN>); 注意:一定不要用while (chomp($line=<MYFILE>)),因为chomp总是返回0值,和while($line=<MYFILE>)是不同的 #! /usr/bin/perl 写文件 print/printf 句柄 (字串); 最简单的文件COPY #!/usr/bin/perl sub gotest{ [macg@localhost perltest]$ ./tip.pl -e 文件是否存在 -d 目录是否存在 #!/usr/bin/perl chomp($file=<>); sub gotest{ [macg@localhost perltest]$ ./tip.pl sub gotest{ [macg@localhost perltest]$ ls -F [macg@localhost perltest]$ ./tip.pl if (!-e $file) 如果文件不存在 -z是否为空文件,-s是否非空 [macg@localhost perltest]$ ./tip.pl -l 是否为符号链接 基本文件操作 sub gotest{ [macg@localhost perltest]$ ls [macg@localhost perltest]$ ./tip.pl [macg@localhost perltest]$ ls [macg@localhost perltest]$ ./tip.pl [macg@localhost perltest]$ ls rename("原文件名","新名"); #!/usr/bin/perl sub gotest{ [macg@localhost perltest]$ ls [macg@localhost perltest]$ ./tip.pl [macg@localhost perltest]$ ls 取文件属性,共13个属性 sub gotest{ [macg@localhost perltest]$ ls chomp($file=<>); sub gotest{ copy($tmp[0],$tmp[1]); [macg@localhost perltest]$ ./tip.pl [root@localhost perltest]# ls -F /usr/lib/perl5/5.8.6/File 目录操作 chdir("testdir") || die "$!"; mkdir($dir,0755) || die "$!"; rmdir("testdir") || die "$!"; #!/usr/bin/perl chomp($directory=<>); chomp($choice=<>); &gotest($directory,$choice); sub gotest{ my(@tmp)=@_; if($tmp[1]) { mkdir($tmp[0],0755) || die "$!"; } else { rmdir($tmp[0]) || die "$!"; } } [macg@localhost perltest]$ ./tip.pl newdir 1 [macg@localhost perltest]$ ls -F newdir/ newtest test testdir/ tip.pl* [macg@localhost perltest]$ ./tip.pl testdir 0 Directory not empty at ./tip.pl line 13,<> line 2rmdir的die信息 改变文件属性和所属 (需在root下才能起作用。换句话说,这是必须在ROOT下执行的PERL语句) 本文原始链接:http://www.jbxue.com/article/3153.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 详细步骤:用R语言做文本挖掘
- Delphi应用程序的调试(二)使用断点
- delphi – 需要使用TIdTelnet与telnet服务器交互的示例/演示
- grails – Groovy getProperties()调用1000次不存在的属性调
- delphi xe8 多线程和hash函数 base64 用法
- Golang 使用 Beego 与 Mgo 开发的示例程序
- kylin Build过程问题排查:17 Step Name: Build Cube In-Me
- Delphi的Dll中获取文件路径
- POJ 题目1001 ,hdu1753 (大数,Java简单题)
- [bigdata-033] 互联网金融-大数据架构平台和业务流程