perl 文件操作學習
阿新 • • 發佈:2018-01-20
文件操作 ping erl microsoft bin 語句 -c round per 現有文件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=<f>){
print $line;
}
close f;
結果:你好
我是中國人
或者:print <f>;
結果:你好
我是中國人
<> 為取行操作符
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 文件操作學習