in reply to special directory entries

A slight variation on code from the perlfunc:readdir documentation page ...

opendir (DH, $directory); my @files = grep { -f "$directory/$_" } readdir(DH); close (DH);

Note that this code returns *only* the files from the directory, ignoring both the parent path 'dot' entries and sub-directory entries. If you want the sub-directory entries, change the grep code to read -

my @entries = grep { /^\.+$/ } readdir(DH);

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Replies are listed 'Best First'.
Re: Re: special directory entries
by clintp (Curate) on Dec 04, 2001 at 01:46 UTC
    my @entries = grep { /^\.+$/ } readdir(DH);
    Not to be too pedantic, but this code isn't particularly useful. It gives back all of the entries that consist only of dots (and, well, we know they're there!). I think you meant:
    # Everything that isn't just dots. my @e = grep { ! /^\.+$/ } readdir(DH);
    Because then your explanation matches your code. Or maybe even:
    # Everything that doesn't begin with . or .. (better!) my @e = grep { ! /^\.\.?$/ } readdir(DH);
    Because this would then eliminate "..." as a "system directory entry." (... is great for hiding things)
      You are correct ... I had meant to write ...

      my @entries = grep { ! /^\.+$/ } readdir(DH);

      That's what you get for coding before the morning coffee ... Thanks clintp++

       

      perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'