Perl读写文件简单实例

发布时间:2020-10-01编辑:脚本学堂
本文介绍一个perl读写文件的例子,包括读文件、写文件、追加写入。有需要的朋友,参考下吧。

一个读写文件的perl脚本,代码如下:
 

复制代码 代码示例:

#!/usr/bin/perl
#  perl 读文件
open(FILE,"filename.txt");
my $record;
while($record=<FILE>)
{
print("file record is : $recordn");
}
close(FILE);

#   perl 写文件
# 覆盖写入
open(FILE,">filename.txt");
syswrite(FILE,"This is my write file contentsn");
close(FILE);

# 追加写入
open(FILE,">>filename.txt");
syswrite(FILE,"This is my write file n");
syswrite(FILE,"This is my write file contents n");
close(FILE);