#!/usr/bin/perl use strict; use File::Find; use File::Slurp; package OOFileFind; sub new { my ( $class, $dirs ) = @_; my $self = { dirs => $dirs || [] }; $self->{files} = {}; bless $self, $class; return $self; } sub find { my $self = shift; for my $dir ( @{ $self->{dirs} } ) { File::Find::find( sub { $self->_wantedexample() }, $dir ); } } sub _wantedexample { my $self = shift; return unless /\.pl$/; my $file = $File::Find::name; my @lines = File::Slurp::read_file $file; for (@lines) { if ( my ($module) = /^\s*use\s([\w:]+)/ ) { push @{$self->{modules}{$module}}, $file; push @{$self->{files}{$file}}, $module; } } } package main; my $scanner = OOFileFind->new( [ '.' ] ); $scanner->find(); printf STDERR "seen %d files :\n", scalar keys %{$scanner->{files}}; foreach my $file (sort keys %{$scanner->{files}}) { printf STDERR " $file uses: %s\n", join ' ', @{$scanner->{files}{$file}}; } printf STDERR "seen %d modules :\n", scalar keys %{$scanner->{modules}}; foreach my $module (sort keys %{$scanner->{modules}}) { printf STDERR " $module used in: %s\n", join ' ', @{$scanner->{modules}{$module}}; } =head1 NAME OOFileFind =head1 SYNOPSIS use OOFileFind; my $scanner = OOFileFind->new( [ '.' ] ); $scanner->find(); # now use data collected by the $scanner =head1 DESCRIPTION B of PerlMonks wrote this about using File::Find::find() : I Good questions. OOFileFind module demonstrates a simple code pattern that satisfies above requirements. So, if I (or you) need to scan files in a set of directories and extract some possibly complex data from each text file ... =over 4 =item * Rewrite the callback OOFileFind::_wantedexample() to suit your purpose. You can pass to it any number of arguments via $self and collect any number of result data items into hashes or arrays attached to $self. No global variables are needed. =item * Create an instance OOFileFind, call its find(), and use the data collected by _wantedexample(). =back =head1 SEE ALSO File::Find family of modules on CPAN =head1 LICENSE Copyright (C) 2006 Rudi Farkas. License hereby granted for anyone to use, modify or redistribute this module as they like. =head1 AUTHOR Rudi Farkas