http://www.perlmonks.org?node_id=66983

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (directories)

How do I read a directory?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I read a directory?
by hippo (Bishop) on Sep 17, 2018 at 11:13 UTC

    With Path::Tiny and using the children method:

    use Path::Tiny 0.028; my $dir = path ('/path/to/dir'); # Retrieve all entries except '.' and '..' my @all = $dir->children (); # Alternatively, retrieve a subset - here just the perl scripts my @perl = $dir->children (qr/\.pl$/);
Re: How do you read a directory?
by damian1301 (Curate) on Mar 25, 2001 at 11:50 UTC
Re: How do I read a directory?
by cLive ;-) (Prior) on Mar 30, 2001 at 10:37 UTC

    Use the readdir function. To read only certain files, you can use it with grep. For example:

    my $dir = '/path/to/dir'; # read all entries: opendir DIR, $dir or die "read dir $dir - $!"; my @dir = readdir DIR; closedir DIR; # read only cgi scripts: opendir DIR, $dir or die "read dir $dir - $!"; my @cgi_scripts = grep /\.cgi$/, readdir DIR; closedir DIR; # read all but (UNIX style) hidden entries: opendir DIR, $dir or die "read dir $dir - $!"; my @dir = grep !/^\./, readdir DIR; closedir DIR;
Re: How do you read a directory
by Sherlock (Deacon) on Apr 19, 2001 at 23:20 UTC
    This is very good, but there is one error:

    open DIR, $dir || die $!;
    should be:

    opendir DIR, $dir || die $!;


    - Sherlock
Re: How do you read a directory
by tinman (Curate) on Mar 25, 2001 at 12:17 UTC
    this node could help too..

    yes, shameless self advertisement, I know :o)

    Originally posted as a Categorized Answer.

Re: How do you read a directory
by Anonymous Monk on Mar 27, 2001 at 11:03 UTC
    The tin man answered my question last time I read here.... what happened to his answer?

    Originally posted as a Categorized Answer.