Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Searching for Perl Modules in Files

by Anonymous Monk
on Aug 27, 2010 at 15:36 UTC ( [id://857709]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!
I need to build a report that will show all Perl modules used by all programs used at work. It means that I have to go through a lot of directories and open every Perl script and look for all Perl modules used in the program, its crazy. I know as a start I can use something like this:
use File::Find; find(\&handleFind, '../directory1'); sub handleFind { my $foundFile = $File::Find::name; print "$foundFile\n" if ($foundFile =~ /\.pl?$/i); }

Does anyone here have anything similar that I can start my quest with? Thanks for looking!

Replies are listed 'Best First'.
Re: Searching for Perl Modules in Files
by marto (Cardinal) on Aug 27, 2010 at 15:44 UTC
      I can use "File::Find::Rule" that works good, any sample about how I could use "Module::ScanDeps" to scan these files for any Perl modules?
      use File::Find::Rule; my $directory="../mydir"; # find all the subdirectories of a given directory my @subdirs = File::Find::Rule->directory->in( $directory ); my $rule = File::Find::Rule->new; $rule->file; $rule->name( '*.pl' ); my @files = $rule->in( $directory ); foreach (@files) { print "$_\n"; }

        From the docs of Module::ScanDeps I'd try something like the following (untested):

        # using your @files array as the starting point... my $dependencies = scan_deps( files => \@files, recurse => 1, );
        I got to a point where I can search for "use" and print every line that uses "use" assuming that it is followed by a Perl module, can someone add to this so it can print the name of the file/script been searched and only "grep" a line that has "use modulename;". Otherwise it is grabbing lines with comments with the word "use" in it. That would take care of this, any help?
        Here is the code so far:

        use File::Find::Rule; my $directory="../yourdirectory"; # find all the subdirectories of a given directory my @subdirs = File::Find::Rule->directory->in( $directory ); my $rule = File::Find::Rule->new; $rule->file; $rule->name( '*.pl' ); my @files = $rule->in( $directory ); foreach my $files(@files) { open (FILE, "$files"); while($line= <FILE> ){ print "$line" if grep /\buse\b/, $line; } close FILE; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found