#! /usr/bin/perl

# program to display all the perl modules located within the @INCS collection

use warnings;
use strict;

# explore a directory, displaying any perl modules it contains,
#    then explore its subdirectories
sub explore {
   # get the directory name and open it
   my $dir = $_[0];
   if (-d $dir) {

      # print the names of any modules in this directory
      my @modules = glob("$dir/*.pm");
      my $numMods = scalar @modules;
      if ($numMods > 0) {
         print "\nPerl modules in directory $dir are:\n";
         foreach my $mod (@modules) {
            my @parts = split '\/',$mod;
            my $modname = $parts[-1];
            print "   $modname\n";
         }
      }

      # explore any subdirectories
      opendir (my $handle, $dir)
         or die "Error: could not open dir $dir\n";
      while (my $nextItem = readdir $handle) {
          my $target = "$dir/$nextItem";
          if (-d $target) {
             if (($nextItem ne ".") and ($nextItem ne "..")) {
                explore($target);
             }
          }
      }
   }
}

# ------ main ------
print "\@INC search locations and contained modules are:\n";
foreach my $dir (@INC) {
   explore($dir);
}

1;
