in reply to reading from directory

You're right on track here. The way I usually do this is as follows:

opendir DIR, "MainDir" or die ""; while( $file = readdir(DIR) ) { #do whatever you want to the file } closedir DIR;

One thing to note, this method includes directory names.

If you just want to grep for a string in all .cpp files in the current directory, try using globs (this is still magic to me, btw) like so:

while( $file = <*.cpp> ) { #do something to the file }

~CubicSpline
"No one tosses a Dwarf!"

Replies are listed 'Best First'.
Re: Re: reading from directory
by moodster (Hermit) on Aug 20, 2002 at 08:32 UTC
    Since there is More Than One Way To Do It, you could also go for the object-oriented approach using DirHandle:
    use DirHandle; my $dh = new DirHandle; $dh->open( 'MainDir' ); foreach ( grep /\.cpp$/, $dh->read ) { # do stuff } $dh->close;
    The DirHandle module is a wrapper around opendir and its relatives, so this is essentially the same as CubicSplines answer, just another way of writing it.

    Cheers,
    --Moodster