Perl学习笔记之文件操作
网络编程
Perl对文件的操作,跟其它的语言类似,无非也就是打开,读与写的操作。
1. 打开文件
#! c:/perl/bin/perl -w use utf8; use strict; use warnings; my $filename = 'test.txt'; # 或者用绝对路径,如: c:/perl/Learn/test.txt if(open(MYFILE,$filename)) # MYFILE是一个标志 { printf "Can open this file:%s!", $filename; close(MYFILE); } else{ print "Can't open this file!"; }
2. 读取文件
#! c:/perl/bin/perl -w use utf8; use strict; use warnings; my $filename = 'test.txt'; if(open(MYFILE,$filename)) { my @myfile = <MYFILE>; #如果要读取多行,用此方法,如果只读取一行为:$myfile = <>; my $count = 0; #要读取的行数,初始值为0 printf "I have opened this file: %sn", $filename; while($count < @myfile){ #遍历 print ("$myfile[$count]n"); #注意此种写法. $count++; } close(MYFILE); } else{ print "I can't open this file!"; } exit;
3. 写入文件
#! c:/perl/bin/perl -w use utf8; use strict; use warnings; my $filename = 'test.txt'; if(open(MYFILE,">>".$filename)) #此种写发,添加不删除 { #此种写法,重写文件内容 MYFILE,">".$filename print MYFILE "Write File appending Testn"; close(MYFILE); } else{ print "I can't open this file!"; } exit;
About Perl(翻译Perl官网介绍)
强大,稳定,成熟,可移植性Perl5是一门有着26年历史的,功能强大的、性能丰富的编程语言。Perl能在超过100种的平台上运行,从便携式设备到大型主机。同
Perl实现高水线算法(解决多值比较问题方法)
"高水线"算法:大水过后,最后一波浪消退时,高水线会标示出所见过的最高水位。下面看下"高水线"算法在Perl中的运用。#!/usr/bin/perl;useutf8;submax{my($max_so
Perl中捕获警告信息、异常信息并写入日志详解
虽然建议在每个Perl脚本和模块中开启警告,可是你又不想用户看到Perl发出的警告。一方面你想在代码前面使用usewarnings作为你的安全网,另一方面,通
编辑:一起学习网
标签:水线,文件,此种,算法,写法