Perl 'readdir' Function Result Order?

I run Perl on Windows and I get a list of all the files in the directory using readdir and storing the result in an array. The first two elements in the array always appear "." and "..". Is this order guaranteed (if the operating system does not change)?

I would like to do the following to remove these values:

my $directory = 'C:\\foo\\bar';

opendir my $directory_handle, $directory 
    or die "Could not open '$directory' for reading: $!\n";

my @files = readdir $directory_handle;
splice ( @files, 0, 2 ); # Remove the "." and ".." elements from the array

But I am worried that this may be unsafe. All the solutions I've seen use regular expressions or instructions for each element in the array, and I would prefer not to use any of these approaches if I don't need it. Thoughts?

+4
source share
2 answers

readdir. ...

, opendir.

, , . , .

.

my @dirs = grep { !/^\.{1,2}\z/ } readdir $dh;

my @dirs = grep { $_ ne '.' && $_ ne '..' } readdir $dh;

, Path::Tiny->children , . , grep . ... Path:: Tiny Perl.

+10

perlmonks 2001 , Perl

readdir Unix . . "" "-", .

, ( perl -U , , ), fsck , . Oops, dot dotdot !

, , . , - , Perl , .

+9

All Articles