Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: reading from directory

by CubicSpline (Friar)
on Aug 19, 2002 at 23:12 UTC ( [id://191335]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://191335]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2024-04-23 16:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found