#!/usr/bin/env perl
use strict;
use warnings;
use 5.006;
use Module::Path qw(module_path);
use Getopt::Long qw(GetOptions);
use Pod::Usage qw(pod2usage);

GetOptions(\my %opts, 'help|h|?', 'quiet|q', 'full|f')
  or pod2usage 2;

if ($opts{help}) {
    pod2usage(
        -verbose => 2,
        -exitval => 0,
    )
}
elsif (!@ARGV) {
    pod2usage(
        -message => 'No module specified',
        -verbose => 1,
    );
}

my $all_found = 1;

for my $module (@ARGV) {
    my $path = module_path($module);
    if (!defined($path)) {
        $all_found = 0;
        print "$module not found\n" unless $opts{quiet};
    }
    else {
        print "$module " if $opts{full};
        print "$path\n";
    }
}

my $status = $all_found ? 0 : 1;
exit $status;

=head1 NAME

mpath - display the full path to a perl module (installed locally)

=head1 USAGE

  mpath [OPTIONS] MODULE [MODULE ...]

=head1 EXAMPLE

  % mpath Module::Path
  /usr/local/lib/perl5/site_perl/5.16.0/Module/Path.pm

  % mpath --full Module::Path
  Module::Path /usr/local/lib/perl5/site_perl/5.16.0/Module/Path.pm

  % mpath Moose Moo
  /usr/local/lib/perl5/site_perl/5.16.0/darwin-2level/Moose.pm
  /usr/local/lib/perl5/site_perl/5.16.0/Moo.pm

=head1 DESCRIPTION

mpath displays the full path to a perl module on the local system.
It uses the C<module_path()> function from L<Module::Path> to get the path.

If one of the module wasn't found, mpath will exit with the exit code 1 and
print the following message (you can silence it with the option B<--quiet>):

  % mpath Foo::Bar
  Foo::Bar not found

or:

  % mpath Moose Foo::Bar
  /usr/local/lib/perl5/site_perl/5.16.0/darwin-2level/Moose.pm
  Foo::Bar not found

=head1 OPTIONS

=over

=item B<-h>, B<-?>, B<--help>

Print this help message and exit.

=item B<-f>, B<--full>

Print module name.

=item B<-q>, B<--quiet>

Don't print any error when one of the module requested could not be found.

=back

=head1 SEE ALSO

L<Module::Path>

=head1 AUTHOR

Neil Bowers E<lt>neilb@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Neil Bowers E<lt>neilb@cpan.orgE<gt>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.