Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

mutew

By: a guest on May 30th, 2009  |  syntax: Perl  |  size: 1.30 KB  |  views: 732  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. use File::Find;
  6.  
  7. my $modPattern = shift;
  8. my @match;
  9.  
  10. find( { wanted => \&findModules,
  11.         follow => 1,
  12.         no_chdir => 1,
  13.       },
  14.       ## Do not include the '.' directory in the file search,
  15.       ## This is to prevent issues with recursing soft-links
  16.       ## TODO: fix the softlink issue outlined above
  17.       grep { $_ !~ '^\.$' } @INC
  18. );
  19.  
  20. eval { defined $modPattern && "" =~ /$modPattern/ };
  21. die "Invalid pattern : $modPattern\n" if $@;
  22.  
  23. sub findModules {
  24.     my $fullPath = $File::Find::name;
  25.     my $regExp = join "|", @INC;
  26.    
  27.     if ( -f $fullPath && m{\.pm$} ) {
  28.         # Format the perl module to its :: eq name
  29.         my $modName = $fullPath;
  30.         # Replace the leading directory name and trailing .pm extension
  31.         $modName =~ s/^($regExp)[\\\/](.+)\.pm/$2/;
  32.         # Change all separators to ::
  33.         $modName =~ s/[\/\\]/::/g;
  34.        
  35.         # Check the module name against the user-supplied expression
  36.         # if any, else just populate it into @match
  37.         if ( defined $modPattern ) {
  38.             push @match, $modName
  39.               if $modName =~ m{$modPattern}i;
  40.         }
  41.         else {
  42.             push @match, $modName;
  43.         }
  44.     }
  45. }
  46.  
  47. print join "\n", @match;
  48. print "\n" if @match;