这两个代码片段有什么区别?
open (MYFILE, '>>data.txt');
open (MYFILE, '>data.txt');
最佳答案
open (MYFILE, '>>data.txt')
— 打开 data.txt
,保留原始数据,从末尾追加数据。 open (MYFILE, '>data.txt')
— 打开 data.txt
,删除里面的所有内容,从头开始写入数据。 来自
perldoc -f open
:If MODE is
'<'
or nothing, the file is opened for input. If MODE is'>'
, the file is truncated and opened for output, being created if necessary. If MODE is'>>'
, the file is opened for appending, again being created if necessary.
它源于 shell 的用法,
cmd < file.txt
将文件复制到标准输入中,cmd > file.txt
将标准输出写入文件,和 cmd >> file.txt
将标准输出附加到文件末尾。 关于perl - Perl 中 '>>' 和 '>' 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3004324/