Perl programming exercises - file traversal operation

Some Knowledge Points:

use Cwd refer to the current directory

cwd: current directory

shift: acquiring location parameters;

chdir Change directory location

opendir Open directory

closedir Close Directory

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('.');


Guess you like

Origin blog.51cto.com/yuweibing/2458522