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

Hi guys,

Im a novice to PERL and find the Active PERL API conventions somewhat confusing. Im trying to read the contents of a directory into an array for processing.

Can anyone help me....................?????

Pretty please with sugar on top....

Thanks v. much.

Pete.

Edit by tye

  • Comment on How to read contents of directory into an array?

Replies are listed 'Best First'.
Re: Help Me Please!
by c-era (Curate) on Aug 06, 2001 at 18:20 UTC
    You should lookup the functions opendir and readdir. Here is an example:
    my @files; opendir (DIR,"my/dir") || die $!; @files = readdir (DIR); closedir (DIR) || die $!;
Re: Help Me Please!
by physi (Friar) on Aug 06, 2001 at 18:24 UTC
    look at  perldoc -f opendir and perldoc -f readdir
    This should help you.
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
Re: Help Me Please!
by kilinrax (Deacon) on Aug 06, 2001 at 18:22 UTC
    The function you're probably looking for is readdir.
Re: Help Me Please!
by IraTarball (Monk) on Aug 06, 2001 at 18:58 UTC
    You might find glob() usefull. Or maybe it's alias <*>. This let's you get the current directory contents with something like:
    my @dir = <*>;

    You can use the glob() function to get the contents of a directory other than the current directory.

    Have a look at perldoc -f glob for more info.

    Ira,

    "So... What do all these little arrows mean?"
    ~unknown

Re: How to read contents of directory into an array?
by TStanley (Canon) on Aug 07, 2001 at 02:04 UTC
    The File::Slurp module from CPAN will also do what you require.
    #!/usr/bin/perl -w use strict; use File::Slurp; my $DIR="directory"; my @Files=read_dir($DIR); my $ln=@Files; for(my $x=0;$x<$ln;$x++){ print"File: $Files[$x]\n"; }
    This module is also available for ActiveState Perl as well, by using the PPM.

    TStanley
    --------
    There's an infinite number of monkeys outside who want to talk to us
    about this script for Hamlet they've worked out
    -- Douglas Adams/Hitchhiker's Guide to the Galaxy