<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Pod::Simple::Search;
use strict;
use warnings;

our $VERSION = '3.45';   ## Current version of this package

BEGIN { *DEBUG = sub () {0} unless defined &amp;DEBUG; }   # set DEBUG level
use Carp ();

our $SLEEPY;
$SLEEPY = 1 if !defined $SLEEPY and $^O =~ /mswin|mac/i;
  # flag to occasionally sleep for $SLEEPY - 1 seconds.

our $MAX_VERSION_WITHIN ||= 60;

#############################################################################

#use diagnostics;
use File::Spec ();
use File::Basename qw( basename dirname );
use Config ();
use Cwd qw( cwd );

#==========================================================================
__PACKAGE__-&gt;_accessorize(  # Make my dumb accessor methods
 'callback', 'progress', 'dir_prefix', 'inc', 'laborious', 'limit_glob',
 'limit_re', 'shadows', 'verbose', 'name2path', 'path2name', 'recurse',
 'ciseen', 'is_case_insensitive'
);
#==========================================================================

sub new {
  my $class = shift;
  my $self = bless {}, ref($class) || $class;
  $self-&gt;init;
  return $self;
}

sub init {
  my $self = shift;
  $self-&gt;inc(1);
  $self-&gt;recurse(1);
  $self-&gt;verbose(DEBUG);
  $self-&gt;is_case_insensitive(-e uc __FILE__ &amp;&amp; -e lc __FILE__);
  return $self;
}

#--------------------------------------------------------------------------

sub survey {
  my($self, @search_dirs) = @_;
  $self = $self-&gt;new unless ref $self; # tolerate being a class method

  $self-&gt;_expand_inc( \@search_dirs );

  $self-&gt;{'_scan_count'} = 0;
  $self-&gt;{'_dirs_visited'} = {};
  $self-&gt;path2name( {} );
  $self-&gt;name2path( {} );
  $self-&gt;ciseen( {} );
  $self-&gt;limit_re( $self-&gt;_limit_glob_to_limit_re ) if $self-&gt;{'limit_glob'};
  my $cwd = cwd();
  my $verbose  = $self-&gt;verbose;
  local $_; # don't clobber the caller's $_ !

  foreach my $try (@search_dirs) {
    unless( File::Spec-&gt;file_name_is_absolute($try) ) {
      # make path absolute
      $try = File::Spec-&gt;catfile( $cwd ,$try);
    }
    # simplify path
    $try =  File::Spec-&gt;canonpath($try);

    my $start_in;
    my $modname_prefix;
    if($self-&gt;{'dir_prefix'}) {
      $start_in = File::Spec-&gt;catdir(
        $try,
        grep length($_), split '[\\/:]+', $self-&gt;{'dir_prefix'}
      );
      $modname_prefix = [grep length($_), split m{[:/\\]}, $self-&gt;{'dir_prefix'}];
      $verbose and print "Appending \"$self-&gt;{'dir_prefix'}\" to $try, ",
        "giving $start_in (= @$modname_prefix)\n";
    } else {
      $start_in = $try;
    }

    if( $self-&gt;{'_dirs_visited'}{$start_in} ) {
      $verbose and print "Directory '$start_in' already seen, skipping.\n";
      next;
    } else {
      $self-&gt;{'_dirs_visited'}{$start_in} = 1;
    }

    unless(-e $start_in) {
      $verbose and print "Skipping non-existent $start_in\n";
      next;
    }

    my $closure = $self-&gt;_make_search_callback;

    if(-d $start_in) {
      # Normal case:
      $verbose and print "Beginning excursion under $start_in\n";
      $self-&gt;_recurse_dir( $start_in, $closure, $modname_prefix );
      $verbose and print "Back from excursion under $start_in\n\n";

    } elsif(-f _) {
      # A excursion consisting of just one file!
      $_ = basename($start_in);
      $verbose and print "Pondering $start_in ($_)\n";
      $closure-&gt;($start_in, $_, 0, []);

    } else {
      $verbose and print "Skipping mysterious $start_in\n";
    }
  }
  $self-&gt;progress and $self-&gt;progress-&gt;done(
   "Noted $$self{'_scan_count'} Pod files total");
  $self-&gt;ciseen( {} );

  return unless defined wantarray; # void
  return $self-&gt;name2path unless wantarray; # scalar
  return $self-&gt;name2path, $self-&gt;path2name; # list
}

#==========================================================================
sub _make_search_callback {
  my $self = $_[0];

  # Put the options in variables, for easy access
  my( $laborious, $verbose, $shadows, $limit_re, $callback, $progress,
      $path2name, $name2path, $recurse, $ciseen, $is_case_insensitive) =
    map scalar($self-&gt;$_()),
     qw(laborious verbose shadows limit_re callback progress
        path2name name2path recurse ciseen is_case_insensitive);
  my ($seen, $remember, $files_for);
  if ($is_case_insensitive) {
      $seen      = sub { $ciseen-&gt;{ lc $_[0] } };
      $remember  = sub { $name2path-&gt;{ $_[0] } = $ciseen-&gt;{ lc $_[0] } = $_[1]; };
      $files_for = sub { my $n = lc $_[0]; grep { lc $path2name-&gt;{$_} eq $n } %{ $path2name } };
  } else {
      $seen      = sub { $name2path-&gt;{ $_[0] } };
      $remember  = sub { $name2path-&gt;{ $_[0] } = $_[1] };
      $files_for = sub { my $n = $_[0]; grep { $path2name-&gt;{$_} eq $n } %{ $path2name } };
  }

  my($file, $shortname, $isdir, $modname_bits);
  return sub {
    ($file, $shortname, $isdir, $modname_bits) = @_;

    if($isdir) { # this never gets called on the startdir itself, just subdirs

      unless( $recurse ) {
        $verbose and print "Not recursing into '$file' as per requested.\n";
        return 'PRUNE';
      }

      if( $self-&gt;{'_dirs_visited'}{$file} ) {
        $verbose and print "Directory '$file' already seen, skipping.\n";
        return 'PRUNE';
      }

      print "Looking in dir $file\n" if $verbose;

      unless ($laborious) { # $laborious overrides pruning
        if( m/^(\d+\.[\d_]{3,})\z/s
             and do { my $x = $1; $x =~ tr/_//d; $x != $] }
           ) {
          $verbose and print "Perl $] version mismatch on $_, skipping.\n";
          return 'PRUNE';
        }

        if( m/^([A-Za-z][a-zA-Z0-9_]*)\z/s ) {
          $verbose and print "$_ is a well-named module subdir.  Looking....\n";
        } else {
          $verbose and print "$_ is a fishy directory name.  Skipping.\n";
          return 'PRUNE';
        }
      } # end unless $laborious

      $self-&gt;{'_dirs_visited'}{$file} = 1;
      return; # (not pruning);
    }

    # Make sure it's a file even worth even considering
    if($laborious) {
      unless(
        m/\.(pod|pm|plx?)\z/i || -x _ and -T _
         # Note that the cheapest operation (the RE) is run first.
      ) {
        $verbose &gt; 1 and print " Brushing off uninteresting $file\n";
        return;
      }
    } else {
      unless( m/^[-_a-zA-Z0-9]+\.(?:pod|pm|plx?)\z/is ) {
        $verbose &gt; 1 and print " Brushing off oddly-named $file\n";
        return;
      }
    }

    $verbose and print "Considering item $file\n";
    my $name = $self-&gt;_path2modname( $file, $shortname, $modname_bits );
    $verbose &gt; 0.01 and print " Nominating $file as $name\n";

    if($limit_re and $name !~ m/$limit_re/i) {
      $verbose and print "Shunning $name as not matching $limit_re\n";
      return;
    }

    if( !$shadows and $seen-&gt;($name) ) {
      $verbose and print "Not worth considering $file ",
        "-- already saw $name as ",
        join(' ', $files_for-&gt;($name)), "\n";
      return;
    }

    # Put off until as late as possible the expense of
    #  actually reading the file:
    $progress and $progress-&gt;reach($self-&gt;{'_scan_count'}, "Scanning $file");
    return unless $self-&gt;contains_pod( $file );
    ++ $self-&gt;{'_scan_count'};

    # Or finally take note of it:
    if ( my $prev = $seen-&gt;($name)  ) {
      $verbose and print
       "Duplicate POD found (shadowing?): $name ($file)\n",
       "    Already seen in ", join(' ', $files_for-&gt;($name)), "\n";
    } else {
      $remember-&gt;($name, $file); # Noting just the first occurrence
    }
    $verbose and print "  Noting $name = $file\n";
    if( $callback ) {
      local $_ = $_; # insulate from changes, just in case
      $callback-&gt;($file, $name);
    }
    $path2name-&gt;{$file} = $name;
    return;
  }
}

#==========================================================================

sub _path2modname {
  my($self, $file, $shortname, $modname_bits) = @_;

  # this code simplifies the POD name for Perl modules:
  # * remove "site_perl"
  # * remove e.g. "i586-linux" (from 'archname')
  # * remove e.g. 5.00503
  # * remove pod/ if followed by perl*.pod (e.g. in pod/perlfunc.pod)
  # * dig into the file for case-preserved name if not already mixed case

  my @m = @$modname_bits;
  my $x;
  my $verbose = $self-&gt;verbose;

  # Shaving off leading naughty-bits
  while(@m
    and defined($x = lc( $m[0] ))
    and(  $x eq 'site_perl'
       or($x =~ m/^pods?$/ and @m == 1 and $shortname =~ m{^perl.*\.pod$}s )
       or $x =~ m{\\d+\\.z\\d+([_.]?\\d+)?}  # if looks like a vernum
       or $x eq lc( $Config::Config{'archname'} )
  )) { shift @m }

  my $name = join '::', @m, $shortname;
  $self-&gt;_simplify_base($name);

  # On VMS, case-preserved document names can't be constructed from
  # filenames, so try to extract them from the "=head1 NAME" tag in the
  # file instead.
  if ($^O eq 'VMS' &amp;&amp; ($name eq lc($name) || $name eq uc($name))) {
      open PODFILE, "&lt;$file" or die "_path2modname: Can't open $file: $!";
      my $in_pod = 0;
      my $in_name = 0;
      my $line;
      while ($line = &lt;PODFILE&gt;) {
        chomp $line;
        $in_pod = 1 if ($line =~ m/^=\w/);
        $in_pod = 0 if ($line =~ m/^=cut/);
        next unless $in_pod;         # skip non-pod text
        next if ($line =~ m/^\s*\z/);           # and blank lines
        next if ($in_pod &amp;&amp; ($line =~ m/^X&lt;/)); # and commands
        if ($in_name) {
          if ($line =~ m/(\w+::)?(\w+)/) {
            # substitute case-preserved version of name
            my $podname = $2;
            my $prefix = $1 || '';
            $verbose and print "Attempting case restore of '$name' from '$prefix$podname'\n";
            unless ($name =~ s/$prefix$podname/$prefix$podname/i) {
              $verbose and print "Attempting case restore of '$name' from '$podname'\n";
              $name =~ s/$podname/$podname/i;
            }
            last;
          }
        }
        $in_name = 1 if ($line =~ m/^=head1 NAME/);
    }
    close PODFILE;
  }

  return $name;
}

#==========================================================================

sub _recurse_dir {
  my($self, $startdir, $callback, $modname_bits) = @_;

  my $maxdepth = $self-&gt;{'fs_recursion_maxdepth'} || 10;
  my $verbose = $self-&gt;verbose;

  my $here_string = File::Spec-&gt;curdir;
  my $up_string   = File::Spec-&gt;updir;
  $modname_bits ||= [];

  my $recursor;
  $recursor = sub {
    my($dir_long, $dir_bare) = @_;
    if( @$modname_bits &gt;= 10 ) {
      $verbose and print "Too deep! [@$modname_bits]\n";
      return;
    }

    unless(-d $dir_long) {
      $verbose &gt; 2 and print "But it's not a dir! $dir_long\n";
      return;
    }
    unless( opendir(INDIR, $dir_long) ) {
      $verbose &gt; 2 and print "Can't opendir $dir_long : $!\n";
      closedir(INDIR);
      return
    }

    # Load all items; put no extension before .pod before .pm before .plx?.
    my @items = map { $_-&gt;[0] }
      sort { $a-&gt;[1] cmp $b-&gt;[1] || $b-&gt;[2] cmp $a-&gt;[2] }
      map {
        (my $t = $_) =~ s/[.]p(m|lx?|od)\z//;
        [$_, $t, lc($1 || 'z') ]
      } readdir(INDIR);
    closedir(INDIR);

    push @$modname_bits, $dir_bare unless $dir_bare eq '';

    my $i_full;
    foreach my $i (@items) {
      next if $i eq $here_string or $i eq $up_string or $i eq '';
      $i_full = File::Spec-&gt;catfile( $dir_long, $i );

      if(!-r $i_full) {
        $verbose and print "Skipping unreadable $i_full\n";

      } elsif(-f $i_full) {
        $_ = $i;
        $callback-&gt;(          $i_full, $i, 0, $modname_bits );

      } elsif(-d _) {
        $i =~ s/\.DIR\z//i if $^O eq 'VMS';
        $_ = $i;
        my $rv = $callback-&gt;( $i_full, $i, 1, $modname_bits ) || '';

        if($rv eq 'PRUNE') {
          $verbose &gt; 1 and print "OK, pruning";
        } else {
          # Otherwise, recurse into it
          $recursor-&gt;( File::Spec-&gt;catdir($dir_long, $i) , $i);
        }
      } else {
        $verbose &gt; 1 and print "Skipping oddity $i_full\n";
      }
    }
    pop @$modname_bits;
    return;
  };;

  local $_;
  $recursor-&gt;($startdir, '');

  undef $recursor;  # allow it to be GC'd

  return;
}


#==========================================================================

sub run {
  # A function, useful in one-liners

  my $self = __PACKAGE__-&gt;new;
  $self-&gt;limit_glob($ARGV[0]) if @ARGV;
  $self-&gt;callback( sub {
    my($file, $name) = @_;
    my $version = '';

    # Yes, I know we won't catch the version in like a File/Thing.pm
    #  if we see File/Thing.pod first.  That's just the way the
    #  cookie crumbles.  -- SMB

    if($file =~ m/\.pod$/i) {
      # Don't bother looking for $VERSION in .pod files
      DEBUG and print "Not looking for \$VERSION in .pod $file\n";
    } elsif( !open(INPOD, $file) ) {
      DEBUG and print "Couldn't open $file: $!\n";
      close(INPOD);
    } else {
      # Sane case: file is readable
      my $lines = 0;
      while(&lt;INPOD&gt;) {
        last if $lines++ &gt; $MAX_VERSION_WITHIN; # some degree of sanity
        if( s/^\s*\$VERSION\s*=\s*//s and m/\d/ ) {
          DEBUG and print "Found version line (#$lines): $_";
          s/\s*\#.*//s;
          s/\;\s*$//s;
          s/\s+$//s;
          s/\t+/ /s; # nix tabs
          # Optimize the most common cases:
          $_ = "v$1"
            if m{^v?["']?([0-9_]+(\.[0-9_]+)*)["']?$}s
             # like in $VERSION = "3.14159";
             or m{\$Revision:\s*([0-9_]+(?:\.[0-9_]+)*)\s*\$}s
             # like in sprintf("%d.%02d", q$Revision: 4.13 $ =~ /(\d+)\.(\d+)/);
          ;

          # Like in sprintf("%d.%s", map {s/_//g; $_} q$Name: release-0_55-public $ =~ /-(\d+)_([\d_]+)/)
          $_ = sprintf("v%d.%s",
            map {s/_//g; $_}
              $1 =~ m/-(\d+)_([\d_]+)/) # snare just the numeric part
           if m{\$Name:\s*([^\$]+)\$}s
          ;
          $version = $_;
          DEBUG and print "Noting $version as version\n";
          last;
        }
      }
      close(INPOD);
    }
    print "$name\t$version\t$file\n";
    return;
    # End of callback!
  });

  $self-&gt;survey;
}

#==========================================================================

sub simplify_name {
  my($self, $str) = @_;

  # Remove all path components
  #                             XXX Why not just use basename()? -- SMB

  if ($^O eq 'MacOS') { $str =~ s{^.*:+}{}s }
  else                { $str =~ s{^.*/+}{}s }

  $self-&gt;_simplify_base($str);
  return $str;
}

#==========================================================================

sub _simplify_base {   # Internal method only

  # strip Perl's own extensions
  $_[1] =~ s/\.(pod|pm|plx?)\z//i;

  # strip meaningless extensions on Win32 and OS/2
  $_[1] =~ s/\.(bat|exe|cmd)\z//i if $^O =~ /mswin|os2/i;

  # strip meaningless extensions on VMS
  $_[1] =~ s/\.(com)\z//i if $^O eq 'VMS';

  return;
}

#==========================================================================

sub _expand_inc {
  my($self, $search_dirs) = @_;

  return unless $self-&gt;{'inc'};
  my %seen = map { File::Spec-&gt;rel2abs($_) =&gt; 1 } @{ $search_dirs };

  if ($^O eq 'MacOS') {
    push @$search_dirs,
      grep { !$seen{ File::Spec-&gt;rel2abs($_) }++ } $self-&gt;_mac_whammy(@INC);
  # Any other OSs need custom handling here?
  } else {
    push @$search_dirs,
      grep { !$seen{ File::Spec-&gt;rel2abs($_) }++ } @INC;
  }

  $self-&gt;{'laborious'} = 0;   # Since inc said to use INC
  return;
}

#==========================================================================

sub _mac_whammy { # Tolerate '.', './some_dir' and '(../)+some_dir' on Mac OS
  my @them;
  (undef,@them) = @_;
  for $_ (@them) {
    if ( $_ eq '.' ) {
      $_ = ':';
    } elsif ( $_ =~ s|^((?:\.\./)+)|':' x (length($1)/3)|e ) {
      $_ = ':'. $_;
    } else {
      $_ =~ s|^\./|:|;
    }
  }
  return @them;
}

#==========================================================================

sub _limit_glob_to_limit_re {
  my $self = $_[0];
  my $limit_glob = $self-&gt;{'limit_glob'} || return;

  my $limit_re = '^' . quotemeta($limit_glob) . '$';
  $limit_re =~ s/\\\?/./g;    # glob "?" =&gt; "."
  $limit_re =~ s/\\\*/.*?/g;  # glob "*" =&gt; ".*?"
  $limit_re =~ s/\.\*\?\$$//s; # final glob "*" =&gt; ".*?$" =&gt; ""

  $self-&gt;{'verbose'} and print "Turning limit_glob $limit_glob into re $limit_re\n";

  # A common optimization:
  if(!exists($self-&gt;{'dir_prefix'})
    and $limit_glob =~ m/^(?:\w+\:\:)+/s  # like "File::*" or "File::Thing*"
    # Optimize for sane and common cases (but not things like "*::File")
  ) {
    $self-&gt;{'dir_prefix'} = join "::", $limit_glob =~ m/^(?:\w+::)+/sg;
    $self-&gt;{'verbose'} and print " and setting dir_prefix to $self-&gt;{'dir_prefix'}\n";
  }

  return $limit_re;
}

#==========================================================================

# contribution mostly from Tim Jenness &lt;t.jenness@jach.hawaii.edu&gt;

sub _actual_filenames {
    my $dir = shift;
    my $fn = lc shift;
    opendir my ($dh), $dir or return;
    return map { File::Spec-&gt;catdir($dir, $_) }
        grep { lc $_  eq $fn } readdir $dh;
}

sub find {
  my($self, $pod, @search_dirs) = @_;
  $self = $self-&gt;new unless ref $self; # tolerate being a class method

  # Check usage
  Carp::carp 'Usage: \$self-&gt;find($podname, ...)'
   unless defined $pod and length $pod;

  my $verbose = $self-&gt;verbose;

  # Split on :: and then join the name together using File::Spec
  my @parts = split /::/, $pod;
  $verbose and print "Chomping {$pod} =&gt; {@parts}\n";

  #@search_dirs = File::Spec-&gt;curdir unless @search_dirs;

  $self-&gt;_expand_inc(\@search_dirs);
  # Add location of binaries such as pod2text:
  push @search_dirs, $Config::Config{'scriptdir'} if $self-&gt;inc;

  my %seen_dir;
  while (my $dir = shift @search_dirs ) {
    next unless defined $dir and length $dir;
    next if $seen_dir{$dir};
    $seen_dir{$dir} = 1;
    unless(-d $dir) {
      print "Directory $dir does not exist\n" if $verbose;
    }

    print "Looking in directory $dir\n" if $verbose;
    my $fullname = File::Spec-&gt;catfile( $dir, @parts );
    print "Filename is now $fullname\n" if $verbose;

    foreach my $ext ('', '.pod', '.pm', '.pl') {   # possible extensions
      my $fullext = $fullname . $ext;
      if ( -f $fullext and $self-&gt;contains_pod($fullext) ) {
        print "FOUND: $fullext\n" if $verbose;
        if (@parts &gt; 1 &amp;&amp; lc $parts[0] eq 'pod' &amp;&amp; $self-&gt;is_case_insensitive() &amp;&amp; $ext eq '.pod') {
          # Well, this file could be for a program (perldoc) but we actually
          # want a module (Pod::Perldoc). So see if there is a .pm with the
          # proper casing.
          my $subdir = dirname $fullext;
          unless (grep { $fullext eq $_  } _actual_filenames $subdir, "$parts[-1].pod") {
            print "# Looking for alternate spelling in $subdir\n" if $verbose;
            # Try the .pm file.
            my $pm = $fullname . '.pm';
            if ( -f $pm and $self-&gt;contains_pod($pm) ) {
              # Prefer the .pm if its case matches.
              if (grep { $pm eq $_  } _actual_filenames $subdir, "$parts[-1].pm") {
                print "FOUND: $fullext\n" if $verbose;
                return $pm;
              }
            }
          }
        }
        return $fullext;
      }
    }

    # Case-insensitively Look for ./pod directories and slip them in.
    for my $subdir ( _actual_filenames($dir, 'pods'), _actual_filenames($dir, 'pod') ) {
      if (-d $subdir) {
        $verbose and print "Noticing $subdir and looking there...\n";
        unshift @search_dirs, $subdir;
      }
    }
  }

  return undef;
}

#==========================================================================

sub contains_pod {
  my($self, $file) = @_;
  my $verbose = $self-&gt;{'verbose'};

  # check for one line of POD
  $verbose &gt; 1 and print " Scanning $file for pod...\n";
  unless( open(MAYBEPOD,"&lt;$file") ) {
    print "Error: $file is unreadable: $!\n";
    return undef;
  }

  sleep($SLEEPY - 1) if $SLEEPY;
   # avoid totally hogging the processor on OSs with poor process control

  local $_;
  while( &lt;MAYBEPOD&gt; ) {
    if(m/^=(head\d|pod|over|item)\b/s) {
      close(MAYBEPOD) || die "Bizarre error closing $file: $!\nAborting";
      chomp;
      $verbose &gt; 1 and print "  Found some pod ($_) in $file\n";
      return 1;
    }
  }
  close(MAYBEPOD) || die "Bizarre error closing $file: $!\nAborting";
  $verbose &gt; 1 and print "  No POD in $file, skipping.\n";
  return 0;
}

#==========================================================================

sub _accessorize {  # A simple-minded method-maker
  shift;
  no strict 'refs';
  foreach my $attrname (@_) {
    *{caller() . '::' . $attrname} = sub {
      use strict;
      $Carp::CarpLevel = 1,  Carp::croak(
       "Accessor usage: \$obj-&gt;$attrname() or \$obj-&gt;$attrname(\$new_value)"
      ) unless (@_ == 1 or @_ == 2) and ref $_[0];

      # Read access:
      return $_[0]-&gt;{$attrname} if @_ == 1;

      # Write access:
      $_[0]-&gt;{$attrname} = $_[1];
      return $_[0]; # RETURNS MYSELF!
    };
  }
  # Ya know, they say accessories make the ensemble!
  return;
}

#==========================================================================
sub _state_as_string {
  my $self = $_[0];
  return '' unless ref $self;
  my @out = "{\n  # State of $self ...\n";
  foreach my $k (sort keys %$self) {
    push @out, "  ", _esc($k), " =&gt; ", _esc($self-&gt;{$k}), ",\n";
  }
  push @out, "}\n";
  my $x = join '', @out;
  $x =~ s/^/#/mg;
  return $x;
}

sub _esc {
  my $in = $_[0];
  return 'undef' unless defined $in;
  $in =~
    s&lt;([^\x20\x21\x23\x27-\x3F\x41-\x5B\x5D-\x7E])&gt;
     &lt;'\\x'.(unpack("H2",$1))&gt;eg;
  return qq{"$in"};
}

#==========================================================================

run() unless caller;  # run if "perl whatever/Search.pm"

1;

#==========================================================================

__END__


=head1 NAME

Pod::Simple::Search - find POD documents in directory trees

=head1 SYNOPSIS

  use Pod::Simple::Search;
  my $name2path = Pod::Simple::Search-&gt;new-&gt;limit_glob('LWP::*')-&gt;survey;
  print "Looky see what I found: ",
    join(' ', sort keys %$name2path), "\n";

  print "LWPUA docs = ",
    Pod::Simple::Search-&gt;new-&gt;find('LWP::UserAgent') || "?",
    "\n";

=head1 DESCRIPTION

B&lt;Pod::Simple::Search&gt; is a class that you use for running searches
for Pod files.  An object of this class has several attributes
(mostly options for controlling search options), and some methods
for searching based on those attributes.

The way to use this class is to make a new object of this class,
set any options, and then call one of the search options
(probably C&lt;survey&gt; or C&lt;find&gt;).  The sections below discuss the
syntaxes for doing all that.


=head1 CONSTRUCTOR

This class provides the one constructor, called C&lt;new&gt;.
It takes no parameters:

  use Pod::Simple::Search;
  my $search = Pod::Simple::Search-&gt;new;

=head1 ACCESSORS

This class defines several methods for setting (and, occasionally,
reading) the contents of an object. With two exceptions (discussed at
the end of this section), these attributes are just for controlling the
way searches are carried out.

Note that each of these return C&lt;$self&gt; when you call them as
C&lt;&lt; $self-&gt;I&lt;whatever(value)&gt; &gt;&gt;.  That's so that you can chain
together set-attribute calls like this:

  my $name2path =
    Pod::Simple::Search-&gt;new
    -&gt; inc(0) -&gt; verbose(1) -&gt; callback(\&amp;blab)
    -&gt;survey(@there);

...which works exactly as if you'd done this:

  my $search = Pod::Simple::Search-&gt;new;
  $search-&gt;inc(0);
  $search-&gt;verbose(1);
  $search-&gt;callback(\&amp;blab);
  my $name2path = $search-&gt;survey(@there);

=over

=item $search-&gt;inc( I&lt;true-or-false&gt; );

This attribute, if set to a true value, means that searches should
implicitly add perl's I&lt;@INC&gt; paths. This
automatically considers paths specified in the C&lt;PERL5LIB&gt; environment
as this is prepended to I&lt;@INC&gt; by the Perl interpreter itself.
This attribute's default value is B&lt;TRUE&gt;.  If you want to search
only specific directories, set $self-&gt;inc(0) before calling
$inc-&gt;survey or $inc-&gt;find.


=item $search-&gt;verbose( I&lt;nonnegative-number&gt; );

This attribute, if set to a nonzero positive value, will make searches output
(via C&lt;warn&gt;) notes about what they're doing as they do it.
This option may be useful for debugging a pod-related module.
This attribute's default value is zero, meaning that no C&lt;warn&gt; messages
are produced.  (Setting verbose to 1 turns on some messages, and setting
it to 2 turns on even more messages, i.e., makes the following search(es)
even more verbose than 1 would make them.)

=item $search-&gt;limit_glob( I&lt;some-glob-string&gt; );

This option means that you want to limit the results just to items whose
podnames match the given glob/wildcard expression. For example, you
might limit your search to just "LWP::*", to search only for modules
starting with "LWP::*" (but not including the module "LWP" itself); or
you might limit your search to "LW*" to see only modules whose (full)
names begin with "LW"; or you might search for "*Find*" to search for
all modules with "Find" somewhere in their full name. (You can also use
"?" in a glob expression; so "DB?" will match "DBI" and "DBD".)


=item $search-&gt;callback( I&lt;\&amp;some_routine&gt; );

This attribute means that every time this search sees a matching
Pod file, it should call this callback routine.  The routine is called
with two parameters: the current file's filespec, and its pod name.
(For example: C&lt;("/etc/perljunk/File/Crunk.pm", "File::Crunk")&gt; would
be in C&lt;@_&gt;.)

The callback routine's return value is not used for anything.

This attribute's default value is false, meaning that no callback
is called.

=item $search-&gt;laborious( I&lt;true-or-false&gt; );

Unless you set this attribute to a true value, Pod::Search will
apply Perl-specific heuristics to find the correct module PODs quickly.
This attribute's default value is false.  You won't normally need
to set this to true.

Specifically: Turning on this option will disable the heuristics for
seeing only files with Perl-like extensions, omitting subdirectories
that are numeric but do I&lt;not&gt; match the current Perl interpreter's
version ID, suppressing F&lt;site_perl&gt; as a module hierarchy name, etc.

=item $search-&gt;recurse( I&lt;true-or-false&gt; );

Unless you set this attribute to a false value, Pod::Search will
recurse into subdirectories of the search directories.

=item $search-&gt;shadows( I&lt;true-or-false&gt; );

Unless you set this attribute to a true value, Pod::Simple::Search will
consider only the first file of a given modulename as it looks thru the
specified directories; that is, with this option off, if
Pod::Simple::Search has seen a C&lt;somepathdir/Foo/Bar.pm&gt; already in this
search, then it won't bother looking at a C&lt;somelaterpathdir/Foo/Bar.pm&gt;
later on in that search, because that file is merely a "shadow". But if
you turn on C&lt;&lt; $self-&gt;shadows(1) &gt;&gt;, then these "shadow" files are
inspected too, and are noted in the pathname2podname return hash.

This attribute's default value is false; and normally you won't
need to turn it on.

=item $search-&gt;is_case_insensitive( I&lt;true-or-false&gt; );

Pod::Simple::Search will by default internally make an assumption
based on the underlying filesystem where the class file is found
whether it is case insensitive or not.

If it is determined to be case insensitive, during survey() it may
skip pod files/modules that happen to be equal to names it's already
seen, ignoring case.

However, it's possible to have distinct files in different directories
that intentionally has the same name, just differing in case, that should
be reported. Hence, you may force the behavior by setting this to true
or false.

=item $search-&gt;limit_re( I&lt;some-regxp&gt; );

Setting this attribute (to a value that's a regexp) means that you want
to limit the results just to items whose podnames match the given
regexp. Normally this option is not needed, and the more efficient
C&lt;limit_glob&gt; attribute is used instead.

=item $search-&gt;dir_prefix( I&lt;some-string-value&gt; );

Setting this attribute to a string value means that the searches should
begin in the specified subdirectory name (like "Pod" or "File::Find",
also expressible as "File/Find"). For example, the search option
C&lt;&lt; $search-&gt;limit_glob("File::Find::R*") &gt;&gt;
is the same as the combination of the search options
C&lt;&lt; $search-&gt;limit_re("^File::Find::R") -&gt; dir_prefix("File::Find") &gt;&gt;.

Normally you don't need to know about the C&lt;dir_prefix&gt; option, but I
include it in case it might prove useful for someone somewhere.

(Implementationally, searching with limit_glob ends up setting limit_re
and usually dir_prefix.)


=item $search-&gt;progress( I&lt;some-progress-object&gt; );

If you set a value for this attribute, the value is expected
to be an object (probably of a class that you define) that has a
C&lt;reach&gt; method and a C&lt;done&gt; method.  This is meant for reporting
progress during the search, if you don't want to use a simple
callback.

Normally you don't need to know about the C&lt;progress&gt; option, but I
include it in case it might prove useful for someone somewhere.

While a search is in progress, the progress object's C&lt;reach&gt; and
C&lt;done&gt; methods are called like this:

  # Every time a file is being scanned for pod:
  $progress-&gt;reach($count, "Scanning $file");   ++$count;

  # And then at the end of the search:
  $progress-&gt;done("Noted $count Pod files total");

Internally, we often set this to an object of class
Pod::Simple::Progress.  That class is probably undocumented,
but you may wish to look at its source.


=item $name2path = $self-&gt;name2path;

This attribute is not a search parameter, but is used to report the
result of C&lt;survey&gt; method, as discussed in the next section.

=item $path2name = $self-&gt;path2name;

This attribute is not a search parameter, but is used to report the
result of C&lt;survey&gt; method, as discussed in the next section.

=back

=head1 MAIN SEARCH METHODS

Once you've actually set any options you want (if any), you can go
ahead and use the following methods to search for Pod files
in particular ways.


=head2 C&lt;&lt; $search-&gt;survey( @directories ) &gt;&gt;

The method C&lt;survey&gt; searches for POD documents in a given set of
files and/or directories.  This runs the search according to the various
options set by the accessors above.  (For example, if the C&lt;inc&gt; attribute
is on, as it is by default, then the perl @INC directories are implicitly
added to the list of directories (if any) that you specify.)

The return value of C&lt;survey&gt; is two hashes:

=over

=item C&lt;name2path&gt;

A hash that maps from each pod-name to the filespec (like
"Stuff::Thing" =&gt; "/whatever/plib/Stuff/Thing.pm")

=item C&lt;path2name&gt;

A hash that maps from each Pod filespec to its pod-name (like
"/whatever/plib/Stuff/Thing.pm" =&gt; "Stuff::Thing")

=back

Besides saving these hashes as the hashref attributes
C&lt;name2path&gt; and C&lt;path2name&gt;, calling this function also returns
these hashrefs.  In list context, the return value of
C&lt;&lt; $search-&gt;survey &gt;&gt; is the list C&lt;(\%name2path, \%path2name)&gt;.
In scalar context, the return value is C&lt;\%name2path&gt;.
Or you can just call this in void context.

Regardless of calling context, calling C&lt;survey&gt; saves
its results in its C&lt;name2path&gt; and C&lt;path2name&gt; attributes.

E.g., when searching in F&lt;$HOME/perl5lib&gt;, the file
F&lt;$HOME/perl5lib/MyModule.pm&gt; would get the POD name I&lt;MyModule&gt;,
whereas F&lt;$HOME/perl5lib/Myclass/Subclass.pm&gt; would be
I&lt;Myclass::Subclass&gt;. The name information can be used for POD
translators.

Only text files containing at least one valid POD command are found.

In verbose mode, a warning is printed if shadows are found (i.e., more
than one POD file with the same POD name is found, e.g. F&lt;CPAN.pm&gt; in
different directories).  This usually indicates duplicate occurrences of
modules in the I&lt;@INC&gt; search path, which is occasionally inadvertent
(but is often simply a case of a user's path dir having a more recent
version than the system's general path dirs in general.)

The options to this argument is a list of either directories that are
searched recursively, or files.  (Usually you wouldn't specify files,
but just dirs.)  Or you can just specify an empty-list, as in
$name2path; with the C&lt;inc&gt; option on, as it is by default.

The POD names of files are the plain basenames with any Perl-like
extension (.pm, .pl, .pod) stripped, and path separators replaced by
C&lt;::&gt;'s.

Calling Pod::Simple::Search-&gt;search(...) is short for
Pod::Simple::Search-&gt;new-&gt;search(...).  That is, a throwaway object
with default attribute values is used.


=head2 C&lt;&lt; $search-&gt;simplify_name( $str ) &gt;&gt;

The method B&lt;simplify_name&gt; is equivalent to B&lt;basename&gt;, but also
strips Perl-like extensions (.pm, .pl, .pod) and extensions like
F&lt;.bat&gt;, F&lt;.cmd&gt; on Win32 and OS/2, or F&lt;.com&gt; on VMS, respectively.


=head2 C&lt;&lt; $search-&gt;find( $pod ) &gt;&gt;

=head2 C&lt;&lt; $search-&gt;find( $pod, @search_dirs ) &gt;&gt;

Returns the location of a Pod file, given a Pod/module/script name
(like "Foo::Bar" or "perlvar" or "perldoc"), and an idea of
what files/directories to look in.
It searches according to the various options set by the accessors above.
(For example, if the C&lt;inc&gt; attribute is on, as it is by default, then
the perl @INC directories are implicitly added to the list of
directories (if any) that you specify.)

This returns the full path of the first occurrence to the file.
Package names (eg 'A::B') are automatically converted to directory
names in the selected directory.  Additionally, '.pm', '.pl' and '.pod'
are automatically appended to the search as required.
(So, for example, under Unix, "A::B" is converted to "somedir/A/B.pm",
"somedir/A/B.pod", or "somedir/A/B.pl", as appropriate.)

If no such Pod file is found, this method returns undef.

If any of the given search directories contains a F&lt;pod/&gt; subdirectory,
then it is searched.  (That's how we manage to find F&lt;perlfunc&gt;,
for example, which is usually in F&lt;pod/perlfunc&gt; in most Perl dists.)

The C&lt;verbose&gt; and C&lt;inc&gt; attributes influence the behavior of this
search; notably, C&lt;inc&gt;, if true, adds @INC I&lt;and also
$Config::Config{'scriptdir'}&gt; to the list of directories to search.

It is common to simply say C&lt;&lt; $filename = Pod::Simple::Search-&gt; new
-&gt;find("perlvar") &gt;&gt; so that just the @INC (well, and scriptdir)
directories are searched.  (This happens because the C&lt;inc&gt;
attribute is true by default.)

Calling Pod::Simple::Search-&gt;find(...) is short for
Pod::Simple::Search-&gt;new-&gt;find(...).  That is, a throwaway object
with default attribute values is used.


=head2 C&lt;&lt; $self-&gt;contains_pod( $file ) &gt;&gt;

Returns true if the supplied filename (not POD module) contains some Pod
documentation.

=head1 SUPPORT

Questions or discussion about POD and Pod::Simple should be sent to the
pod-people@perl.org mail list. Send an empty email to
pod-people-subscribe@perl.org to subscribe.

This module is managed in an open GitHub repository,
L&lt;https://github.com/perl-pod/pod-simple/&gt;. Feel free to fork and contribute, or
to clone L&lt;https://github.com/perl-pod/pod-simple.git&gt; and send patches!

Patches against Pod::Simple are welcome. Please send bug reports to
&lt;bug-pod-simple@rt.cpan.org&gt;.

=head1 COPYRIGHT AND DISCLAIMERS

Copyright (c) 2002 Sean M. Burke.

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

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.

=head1 AUTHOR

Pod::Simple was created by Sean M. Burke &lt;sburke@cpan.org&gt; with code borrowed
from Marek Rouchal's L&lt;Pod::Find&gt;, which in turn heavily borrowed code from
Nick Ing-Simmons' C&lt;PodToHtml&gt;.

But don't bother him, he's retired.

Pod::Simple is maintained by:

=over

=item * Allison Randal C&lt;allison@perl.org&gt;

=item * Hans Dieter Pearcey C&lt;hdp@cpan.org&gt;

=item * David E. Wheeler C&lt;dwheeler@cpan.org&gt;

=back

=cut
</pre></body></html>