前言
本文主要记录一下,perl中的文件操作。
- 文件名通配: glob 函数
- 删除文件: unlink 函数
- 重命名和移动文件: rename 函数
- 修改文件的时间: utime 函数
12 文件操作
#============================
# 文件名通配: glob 函数
#============================
my @files = glob "*.pl *.txt";
foreach(@files){
print $_ . "\n";
}
#============================
# 删除文件: unlink 函数
#============================
my $successful = unlink "a.txt", "b.txt";
foreach my $file (glob "*.txt"){
unlink $file or warn "failed to delete $file: $!\n";
}
#============================
# 重命名和移动文件: rename 函数
#============================
rename "a.txt" "aa.txt";
rename "a.txt" "test/aa.txt";
#============================
# 修改文件的时间: utime 函数
#============================
my $now = time;
my $yesterday = $now - 24*60*60;
utime $now, $yesterday, glob "*.txt";