perl读写文件

perl写文件

注意:perl写文件时,必须使用的是 真实完整路径

my $fileName;
$fileName = "D:\\13.perlLearn\\test01\\report.txt";
open(my $fh , ">", $fileName) or die "Could not open file report.txt $!";
    print $fh "this is my first test, please ignore it,thanks \n";
close $fh;
print "done\n";上文中的路径必须是绝对路径,不论是在linux或者windows上。

perl读取文件内容

my $fileName01;
# $fileName01 = "D:\\13.perlLearn\\test01\\test.txt";
$fileName01 = "test01\\test.txt";
open(my $fh, "<", $fileName01) or die "could not open this file,sir $!";
my @lineList;
@lineList = <$fh>;
foreach(@lineList){
    print $_;
}
close $fh;

读取文件的时候可以使用相对路径,当然,仍旧可以使用绝对路径。

读取的时候先将其放置在list中,而后遍历list获取,这是perl常用的读取方式。

你可能感兴趣的:(perl)