in reply to Re: selcting all files in a given directory except......
in thread selcting all files in a given directory except......

at least for me, the opendir don't return as the first 2 values the '.' and '..'.
readdir ($DIR); # ignore . readdir ($DIR); # ignore ..
the solution of moritz it's more robust
@files = grep {/^(?!\.{1,2}\z)/} readdir($DIR);

Replies are listed 'Best First'.
Re^3: selcting all files in a given directory except......
by grinder (Bishop) on Dec 06, 2007 at 17:10 UTC

    If you want more robust, don't use a regexp to match parent and current directories.

    @files = grep {$_ ne '.' and $_ ne '..'} readdir($D);

    I know one isn't meant to write code that is easy for a monkey to understand, but all the same, I don't think the added complexity of throwing in a couple of more-rarely used regops like \z and (?!...) is worth it in this case.

    This way there is no ambiguity with strange directories like "foo\n.". Also, your original solution may fail if a corrupted filesystem is rebuilt, and the parent and current directories pointers are rewritten elsewhere than positions 1 and 2 in the directory list.

    If you want to go really cross-platform robust, use File::Spec's curdir() and updir() routines.

    • another intruder with the mooring in the heart of the Perl

Re^3: selcting all files in a given directory except......
by ikegami (Patriarch) on Dec 06, 2007 at 18:48 UTC
    Not all directories have . and .. in them, at least in Windows.