Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

findINC

by Rich36 (Chaplain)
on Oct 21, 2001 at 15:40 UTC ( [id://120369]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Script
Author/Contact Info Rich Reuter
Rich36
Description: A script to find modules in the paths listed in "@INC". The user can specify the full module name or just part of it or even use a regex if passed in quotes (i.e. findINC 'File.*')

I find this to be a useful script when trying to determine if I've got a particular module loaded up, but I can't help but feel I'm reinventing the wheel... Is there another way to do this?

Rich36


10/21 - Implemented merlyn's comments. Thanks.
#!/usr/bin/perl -w

######################################################################
+###########
#
# findINC 
# Wed Sep  5 2001
# Rich Reuter
#
# Find a perl module in @INC.  Can match part or all of a module name.
#
######################################################################
+###########

#######################################################
# Setup variables and packages
#######################################################
use strict;
use File::Find;

my @dirs = @INC;
# can use 'push(@dirs,"<dir_name>");' if there are dirs 
# that are used to store modules, but is not part of @INC

my $module;

#######################################################
# MAIN
#######################################################

# Make sure that only one module name has been specified
if ((!$ARGV[0]) || ($ARGV[1])) {
    die qq(Specify one module to search for!\n);
} else {
    $module = $ARGV[0];
}

# Substitute "::" with "/" to build path name
$module =~ s|::|/|g;

find (\&checkForModule, @dirs);

sub checkForModule() {
    my $element = $File::Find::name;
    if ($element =~ $module) {
        # Re-substitute "/" with "::" for printing purposes
        $module =~ s|/|::|g;
        print qq($module matched at $element\n);
    }
}
Replies are listed 'Best First'.
Re: findINC
by merlyn (Sage) on Oct 21, 2001 at 19:22 UTC
    The code
    foreach my $dir(@dirs) { find(\&checkForModule, $dir); }
    can be more easily written as
    find (\&checkForModule, @dirs);
    unless you needed to know specifically which tree you are on, and that's not the case here.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found