Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 515 lines (390 sloc) 11.578 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
#!/usr/bin/env perl
# --------------------------------------
#
# Title: Perl Module Find
# Purpose: Find Perl modules by wildcards.
#
# Name: pmfind
# File: pmfind
# Created: July 21, 2009
#
# Copyright: Copyright 2009 by Shawn H. Corey. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

# --------------------------------------
# Pragmatics

require 5.8.0;

use strict;
use warnings;

use utf8; # Convert all UTF-8 to Perl's internal representation.
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';

# --------------------------------------
# Version
use version; our $VERSION = qv(1.0.0);

# --------------------------------------
# Modules
use Carp;
use Cwd;
use Data::Dumper;
use English qw( -no_match_vars ) ; # Avoids regex performance penalty
use File::Find;
use File::Spec;
use FindBin '$Bin';
use Getopt::Long;
use Pod::Usage;
use POSIX;

use lib $Bin, glob( '~/lib' );

# --------------------------------------
# Configuration Parameters

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
$Data::Dumper::Maxdepth = 0;

# --------------------------------------
# Variables

# Command-line options
my $Local_paths = 1;
my %Displays = ( n=>1, ); # what to display, default names only

# internal variables
my @Local_paths = (); # not empty if not searching local dirs
my @Paths = (); # paths to search from @INC minus local dirs
my @Patterns = (); # patterns to search for
my %Patterns = (); # $Patterns{$pattern} = search_string

my $Pattern = undef; # pattern currently searching for
my $Searching = undef; # path currently searching
my %Modules = (); # $Modules{name} = [ path ]

# Documentation levels
my $DOC_USAGE = 0;
my $DOC_HELP = 1;
my $DOC_VER = 2;
my $DOC_MAN = 3;

# --------------------------------------
# Subroutines

# --------------------------------------
# Name: print_documentation
# Usage: print_documentation( $documentation_level );
# Purpose: Print the usage, help, or man documentation.
# Returns: Does not return.
# Parameters: $documentation_level -- how much documentation to display.
# 0 == usage
# 1 == help
# 2 == version
# other == man
#
sub print_documentation {
  my $level = shift @_ || $DOC_USAGE;

  # print the usage documentation
  if( $level == $DOC_USAGE ){
    pod2usage(
      -exitval => 2,
      -verbose => 99,
      -sections => 'USAGE',
    );
  }

  # print the help documentation
  if( $level == $DOC_HELP ){
    pod2usage(
      -exitval => 2,
      -verbose => 99,
      -sections => 'NAME|VERSION|USAGE|REQUIRED ARGUMENTS|OPTIONS',
    );
  }

  # print the version
  if( $level == $DOC_VER ){
    pod2usage(
      -exitval => 2,
      -verbose => 99,
      -sections => 'VERSION',
    );
  }

  # print the man documentation
  pod2usage(
    -exitval => 2,
    -verbose => 2,
  );
}

# --------------------------------------
# Name: get_cmd_opts
# Usage: get_cmd_opts();
# Purpose: Process the command-line switches.
# Returns: none
# Parameters: none
#
sub get_cmd_opts {
  my $display = '';

  # Check command-line options
  unless( GetOptions(
    'display=s' => \$display,
    'local!' => \$Local_paths,

    usage => sub { print_documentation( $DOC_USAGE ); },
    help => sub { print_documentation( $DOC_HELP ); },
    version => sub { print_documentation( $DOC_VER ); },
    man => sub { print_documentation( $DOC_MAN ); },
  )){
    print_documentation( $DOC_USAGE );
  }

  # figure out what to display
  if( length $display ){
    %Displays = ();
    $Displays{n} = 1 if $display =~ m{ n | \* }imsx; # name
    $Displays{p} = 1 if $display =~ m{ p | \* }imsx; # pod
    $Displays{l} = 1 if $display =~ m{ l | \* }imsx; # listing (full path)
    $Displays{s} = 1 if $display =~ m{ s | \* }imsx; # searched for
  }
}

# --------------------------------------
# Name: get_locals
# Usage: get_locals();
# Purpose: Get the list of local directores not to search.
# Parameters: none
# Returns: none
#
sub get_locals {
  my $home = cwd();

  @Local_paths = ();
  return if $Local_paths;

  my %paths = ();
  for my $path ( '.', glob('~') ){
    chdir $path or next;
    my $dir = cwd();
    push @Local_paths, $dir unless $paths{$dir} ++;
  }

  chdir $home;
  # print '@Local_paths ', Dumper \@Local_paths, \%paths;
  return;
}

# --------------------------------------
# Name: get_paths
# Usage: get_paths();
# Purpose: Get the real path for @INC dirs.
# Parameters: none
# Returns: none
#
sub get_paths {
  my $home = cwd();

  my %paths = ();
  for my $path ( @INC ){
    chdir $path or next;

    my $dir = cwd();
    next if grep { $dir =~ m{ \A $_ }msx } @Local_paths;

    push @Paths, $dir unless $paths{$dir} ++;
  }

  chdir $home;
  # print '@Paths ', Dumper \@Paths, \%paths;
  return;
}

# --------------------------------------
# Name: get_patterns
# Usage: get_patterns();
# Purpose: Get the module name patterns to search for.
# Parameters: none
# Returns: none
#
sub get_patterns {

  @Patterns = ();
  for my $arg ( @ARGV ){
    my $item = $arg;

    my @parts = split m{\:\:}msx, $item;
    for my $part( @parts ){
      $part =~ s{ ([^\w\*\:]) }{\\$1}gmsx;
      $part =~ s{ \* }{.*}gmsx;
    }
    # print '@parts ', Dumper \@parts;
    $item = File::Spec->catdir( @parts );

    # word boundaries
    $item =~ s{ (\w+) }{\\b$1\\b}gmsx;
    $item =~ s{ \\b\.\*\\b }{\\b}gmsx;
    $item =~ s{ \.\*\\b }{.*}gmsx;
    $item =~ s{ \\b\.\* }{.*}gmsx;

    my $pattern = qr/\A$item\.p(?:m|od)\z/i;
    push @Patterns, $pattern;
    $Patterns{$pattern} = $arg;
  }

  # print '@Patterns ', Dumper \@Patterns;
  # print '%Patterns ', Dumper \%Patterns;
  return;
}

# --------------------------------------
# Name: find_module
# Usage: find_module();
# Purpose: Find the module.
# Parameters: none
# Returns: none
#
sub find_module {

  ( my $tail = $File::Find::name ) =~ s{ \A $Searching \/ }{}msx;
  return unless $tail =~ $Pattern;

  $tail =~ s{ \. p(?:m|od) \z }{}msx;
  $tail =~ s{ \/ }{::}gmsx;

  push @{ $Modules{$tail} }, $File::Find::name;
  return;
}

# --------------------------------------
# Name: show_modules
# Usage: show_modules();
# Purpose: Output the modules
# Parameters: none
# Returns: none
#
sub show_modules {
  my $indent = 0;

  # print '%Modules ', Dumper \%Modules;

  if( $Displays{s} ){
    print "$Patterns{$Pattern}\n";
    $indent ++;
  }

  my %seen = ();
  for my $name ( sort { lc($a) cmp lc($b) || $a cmp $b } keys %Modules ){
    next if $seen{$name} ++;
    my @listings = @{ $Modules{$name} };
    my @paths = ();
    if( $Displays{n} ){
      push @paths, grep { m{ \. pm \z }msx } @listings;
    }
    if( $Displays{p} ){
      push @paths, grep { m{ \. pod \z }msx } @listings;
    }
    if( @paths ){
      print "\t" x $indent, $name, "\n";
      $indent ++;
    }
    if( $Displays{l} ){
      @paths = @listings unless @paths;
      for my $path ( @paths ){
        print "\t" x $indent, $path, "\n";
      }
    }
    $indent -- if @paths;
  }

  return;
}

# --------------------------------------
# Name: search
# Usage: search();
# Purpose: Search for the modules.
# Parameters: none
# Returns: none
#
sub search {

  for my $pattern ( @Patterns ){
    $Pattern = $pattern;
    for my $searching ( @Paths ){
      $Searching = $searching;
      find( \&find_module, $Searching );
    }
    show_modules();
  }

  return;
}

# --------------------------------------
# Main

get_cmd_opts();

get_locals();
get_paths();

get_patterns();
search();

__DATA__
__END__

=head1 NAME

pmfind - Find Perl modules by wildcards.

=head1 VERSION

This document refers to pmfind version 1.0.0

=head1 USAGE

pmfind [<options>] <module_wildcard> ...
pmfind --usage|help|version|man

=head1 REQUIRED ARGUMENTS

=over 4

=item <module_wildcard> ...

One or more module name to search for.
Can include the wildcard, '*', for zero, one, or more characters.

=back

=head1 OPTIONS

=over 4

=item --display=(l|n|p|s|\*)*

A string indicating what to display.
It has one or more of the following.

=over 4

=item l

The listing, that is, the full path.

=item n

The name of the module.

=item p

The POD file, if any.

=item s

The searched for pattern.

=item *

Display all.

=back

=item --[no]local

Include local directories in the search.
Default is to include.

=item --usage

Print a brief usage message.

=item --help

Print usage, required arguments, and options.

=item --version

Print the version number.

=item --man

Print the manual page.

=back

=head1 DESCRIPTION

Searches the @INC directories for the requested modules.
The search is case-insensitive.
The wildcard character, '*', means zero, one, or more characters.

=head1 DIAGNOSTICS

=head1 CONFIGURATION AND ENVIRONMENT

=head1 README

This Perl script finds modules accessible via @INC. It uses
shell expansions. It is case insensitive.

=head1 SCRIPT CATEGORIES

CPAN

=head1 PREREQUISITES

=head1 COREQUISITES

=head1 OSNAMES

=head1 INCOMPATIBILITIES

=head1 BUGS AND LIMITATIONS

Has problems deciding what is a local directory on Windows.

=head1 SEE ALSO

=head1 ORIGINAL AUTHOR

Shawn H. Corey shawnhcorey@gmail.com

=head2 Contributing Authors

(Insert your name here if you modified this program or its documentation.
Do not remove this comment.)

=head1 COPYRIGHT & LICENCES

Copyright 2009 by Shawn H. Corey. All rights reserved.

=head2 Software Licence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

=head2 Document Licence

Permission is granted to copy, distribute and/or modify this document under the
terms of the GNU Free Documentation License, Version 1.2 or any later version
published by the Free Software Foundation; with the Invariant Sections being
ORIGINAL AUTHOR, COPYRIGHT & LICENCES, Software Licence, and Document Licence.

You should have received a copy of the GNU Free Documentation Licence
along with this document; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

=head1 ACKNOWLEDGEMENTS

=cut
Something went wrong with that request. Please try again.