glob 函数实现通配
- #! /usr/bin/perl
- chdir "/etc";
- my @files = glob "*.conf";
- print "@files\n";
0
目录句柄,使用 opendir readdir closedir
例:
#! /usr/bin/perl
opendir ETC, "/root";
foreach $file (readdir ETC) {
print "$file\n";
}
closedir ETC;
例:
#! /usr/bin/perl
opendir ETC, "/root";
foreach $file (readdir ETC) {
print "$file\n";
}
closedir ETC;
0
使用rename把ttt目录下所有.pl结尾的文件重命名为.perl结尾的文件
- chdir "ttt/";
- foreach $file (glob "*.pl") {
- my $newfile = $file;
- $newfile =~ s/\.pl$/.pelr/;
- if ( -e $newfile ) {
- warn "can't rename $file to $newfile: $newfile exists\n";
- }elsif (rename $file, $newfile) {
- }else {
- warn "rename $file to $newfile failed: $!\n";
- }
- }
编辑回复