大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> CGI专区 -> Perl教学 第十一篇 文件系统之三

Perl教学 第十一篇 文件系统之三

时间: 2021-07-31 作者:daque

运转截止如次:$ program file1 file2this is a line from the first file.here is the last line of the first file.-- end of current file --this is a line from the second and last file.here is the last line of the last file.--end of current file --$ 底下把eof改为eof(),第二个步调为:1: #!/usr/local/bin/perl2: 3: while ($line = <>) {4: print ($line);5: if (eof()) {6: print ("-- end of output --\n");7: }8: } 运转截止如次:$ program file1 file2this is a line from the first file.here is the last line of the first file.this is a line from the second and last file.here is the last line of the last file.-- end of output --$ 这时候,惟有一切文献都读过了,eof()才归来真,即使不过多个文献中前几个的结束,归来值为假,由于再有要读取的输出。9)转弯抹角文献变量对于上述各因变量open, close, print, printf, write, select和eof,都不妨用大略变量来包办文献变量,这时候,大略变量中所存贮的字符串就被看作文献变量名,底下即是如许一个例子,此例很大略,就迷惑释了。须要指出的是,因变量open, close, write, select和eof还承诺用表白式来代替文献变量,表白式的值必需是字符串,被用作文献变量名。1: #!/usr/local/bin/perl2: 3: &open_file("infile", "", "file1");4: &open_file("outfile", ">", "file2");5: while ($line = &read_from_file("infile")) {6: &print_to_file("outfile", $line);7: }8: 9: sub open_file {10: local ($filevar, $filemode, $filename) = @_;11: 12: open ($filevar, $filemode . $filename) ||13: die ("can't open $filename");14: }15: sub read_from_file {16: local ($filevar) = @_;17: 18: <$filevar>;19: }20: sub print_to_file {21: local ($filevar, $line) = @_;22: 23: print $filevar ($line);24: } 2、跳过和复读数据因变量名 seek 挪用语法 seek (filevar, distance, relative_to); 证明 在文献中向前/后挪动,有三个参数:1、filevar,文献变量2、distance,挪动的字节数,正数向前挪动,负数往回挪动3、reletive_to,值可为0、1或2。为0时,从文献头发端挪动,为1时,对立于暂时场所(将要读的下一条龙)挪动,为2时,对立于文献结束挪动。运转胜利归来真(非零值),波折则归来零,常与tell因变量适用。 因变量名 tell 挪用语法 tell (filevar); 证明 归来从文献头到暂时场所的隔绝。提防:1、seek和tell不许用来指向弹道的文献变量。2、seek和tell中文献变量参数可运用表白式。 3、体例读写因变量因变量名 read 挪用语法 read (filevar, result, length, skipval); 证明 read因变量安排得与unix的fread因变量等效,不妨读取大肆长度的字符(字节)惠存一个大略变量。其参数有四个:1、filevar:文献变量2、result:存贮截止的大略变量(或数组元素)3、length:读取的字节数4、skipval:可选项,指定读文献之前跳过的字节数。归来值为本质读取的字节数,即使已到了文献结束,则归来零,即使堕落,则归来空串。 因变量名 sysread 挪用语法 sysread (filevar, result, length, skipval); 证明 更快的读取数据,与unix因变量read等效,参数与read沟通。 因变量名 syswrite 挪用语法 syswrite (filevar, data, length, skipval); 证明 更快的写入数据,与unix因变量write等效,参数:1、filevar:将要写入的文献2、data:存贮要写入数据的变量3、length:要写入的字节数4、skipval写操纵之前跳过的字节数。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.