Perl 编程练习-文件遍历操作
作者:互联网
几个知识点:
use Cwd 引用当前目录
cwd:当前目录
shift:获取位置参数;
chdir 改变目录位置
opendir 打开目录
closedir 关闭目录
foreach 。。。next
#!perl -w use strict; use Cwd; // sub scanDirectory { my $workdir=shift; my $startdir=cwd; chdir $workdir or die "Unable to enter the $workdir dir!\n"; opendir my $DIR,'.' or die "can't open the dir\n"; my @names = readdir $DIR or die "can't readdir\n"; closedir $DIR; foreach my $name(@names){ next if($name eq '.'); next if($name eq '..'); if(-d $name){ scanDirectory($name); next; } if($name eq 'core.txt'){ print "find a core file! Path:". cwd ."\n"; next; } } chdir $startdir or die "Unable to enter the $startdir dir!\n"; } &scanDirectory('.');
标签:遍历,name,die,编程,next,workdir,Perl,my,DIR 来源: https://blog.51cto.com/yuweibing/2458522