#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use File::Spec::Functions 'catfile';
my %args;
GetOptions( \%args, 'no-require|n', 'help|h' ) or exit;
@ARGV = grep { defined } @ARGV;
if ( $args{help} || !@ARGV ) {
print <<'EOF';
USAGE: mwhere Module [ ... ]
EXAMPLES:
mwhere Carp # show location of Carp.pm
mwhere --no-require Carp # ditto, but no "require"
mwhere -n Carp # ditto
mwhere Carp CGI # show both Carp.pm and CGI.pm
EOF
exit;
}
for my $mod (@ARGV) {
my @parts = split /::/, $mod;
my $source;
if ( $args{'no-require'} ) {
$parts[-1] .= '.pm';
for my $inc ( @INC ) {
my $file = catfile( $inc, @parts );
if ( -e $file ) {
$source = $file;
last;
}
}
unless ( $source ) {
warn "failed to find $mod\n";
next;
}
}
else {
eval "require $mod" or warn "failed to require '$mod': $@" and next;
my $inc = join( '/', @parts ) . '.pm';
$source = $INC{$inc};
}
print "$source\n";
}