perl txt family 语句 rap padding pad msu orm
现有文件 hello.txt, 内容为:"你好'\n'我是中国人"
1, 打开文本 hello.txt
# ! /usr/bin / perl open f,
"hello.txt";
f 为文件句柄,指向打开的文件
2,逐行读取文本 hello.txt
#!/usr/bin/perl
open f,"<hello.txt";
while($line=){
print $line;
}
close f;
结果:你好
我是中国人
或者:print ;
结果:你好
我是中国人
<> 为取行操作符
3,向 a.txt 中写入内容
#!/usr/bin/perl
open f,">a.txt";
print f "hello,world\nsee you\n";
close f;
如果 a.txt 原本有内容,则原内容将会被擦除,a.txt 的内容为
hello,
world see you
4, 向 a.txt 中追加内容
#!/usr/bin/perl
open f,">>a.txt";
print f "thank you\n";
close f;
a.txt 的内容为
hello,world
see you
thank you
print 相当于写命令,别丢了语句结尾的 ";"
perl 文件操作
来源: http://www.bubuko.com/infodetail-2468747.html