How to ignore the single and double dot entries in Perl's readdir?

后端 未结 5 945
甜味超标
甜味超标 2021-01-21 05:04

Following up from here: Perl Imgsize not working in loop? I have another question - how do I not let perl list the single and double dot entries when it reads the files in a dir

5条回答
  •  情深已故
    2021-01-21 05:26

    I'm going to suggest something else - don't use readdir and instead use glob.

    my @dirlist = glob ( "$dir/*.jpg" ); 
    

    And then you'll get a list of paths to files matching that spec. This is particularly useful if you're doing:

    foreach my $file ( glob ( "/path/to/file/*.jpg" ) ) {
         open ( my $input, '<', $file ) or die $!;
    }
    

    Where with readdir you'll only get a filename, and have to reconstruct the path yourself.

提交回复
热议问题