How do I distinguish a file from a directory in Perl?

后端 未结 6 1173
天涯浪人
天涯浪人 2020-12-31 01:54

I\'m trying to traverse through all the subdirectories of the current directory in Perl, and get data from those files. I\'m using grep to get a list of all files and folder

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 02:00

    my $dh = opendir(".");
    my @entries = grep !/^\.\.?$/, readdir($dh);
    closedir $dh;
    
    foreach my $entry (@entries) {
        if(-f $entry) {
            # $entry is a file
        } elsif (-d $entry) {
            # $entry is a directory
        }
    }
    

提交回复
热议问题