campbell has asked for the wisdom of the Perl Monks concerning the following question:

I have an array of files, and I would like to tell if members of this array are directories or not,
and if they are, open them and do something with the contents.

I tried doing it this way:

foreach $n(@array) { if (opendir DIRHANDLE, $n) { do something with DIRHANDLE; } }

...but this didn't work (the progam just hung). Can anyone help?

TIA, Campbell

Replies are listed 'Best First'.
Re: directory?
by broquaint (Abbot) on Jul 01, 2003 at 11:26 UTC
    Use the -d e.g
    foreach $n(@array) { if (-d $n) { ## do stuff } }
    See. the file test manpage for more info.
    HTH

    _________
    broquaint

      Or even:
      foreach $n (grep { -d } @array) { ## do stuff }
        Or even:
        do {'stuff'} for grep -d, @array;
        or maybe pass the buck to a subroutine:
        some_sub() for grep -d, @array;

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: directory?
by Tomte (Priest) on Jul 01, 2003 at 11:27 UTC

    I suppose a deeper look into what perldoc -f '-X' has to offer you would help you a lot ;)

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: directory?
by gjb (Vicar) on Jul 01, 2003 at 11:27 UTC

    It's easy to test whether something is a directory using the -d function. See the documentation on functions for more details.

    Hope this helps, -gjb-

Re: directory?
by campbell (Beadle) on Jul 01, 2003 at 14:20 UTC
    Thanks, folks

    Campbell