in reply to Easy way to search files?

Take a look at File::Find and File::Find::Rule. From the documentation of the latter:

use File::Find::Rule; + # find all the subdirectories of a given directory + my @subdirs = File::Find::Rule->directory->in( $directory ); + + # find all the .pm files in @INC + my @files = File::Find::Rule->file() + ->name( '*.pm' ) + ->in( @INC );

Once you have found the files you are interested, just open them and use m// (or whatever) to search for what you're interested in.

Replies are listed 'Best First'.
Re: Re: Easy way to search files?
by Juerd (Abbot) on Jan 06, 2003 at 13:12 UTC

    I wonder why people choose the OO interface for File::Find::Rule.

    Personally, I prefer its functional interface. To demonstrate how clean it is, I'm rewriting the code you pasted from the documentation:

    use File::Find::Rule qw(find); my @subdirs = find 'directory', in => $directory; my @files = find 'file', name => '*.pm', in => \@INC;
    Would you choose the OO interface? Why?

    Another nice syntax for exactly the same is find file => (name => '*.pm', in => \@INC).

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      Would you choose the OO interface?

      Yes :-)

      Why?

      Because:

      • I find the OO style more readable.
      • Most of the code I write is OO. Using the OO API to File::Find::Rule saves me switching mental models.
      • Sometimes the intermediate objects are useful. You can use it to generate rules at runtime based on other conditions. This can be harder using the functional style.
Re^2: Easy way to search files?
by Aristotle (Chancellor) on Jan 06, 2003 at 14:24 UTC
    Once you have found the files you are interested, just open them and use m// (or whatever) to search for what you're interested in.

    Or use File::Find::Rule's grep predicate if you're interested in which files match (as the poster is) rather than exactly what they contain. :)

    The following exactly fulfills the poster's spec:

    my @files = File::Find::Rule ->file() ->grep(qr/\Q$word\E/) ->in(@directory);

    Makeshifts last the longest.

Re: Re: Easy way to search files?
by Hofmator (Curate) on Jan 06, 2003 at 13:38 UTC
    On the topic of File::Find::Rule I'd like to mention that this module was featured in the Perl Advent Calender here (off-site link).

    -- Hofmator