<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package ExtUtils::Installed;

use 5.00503;
use strict;
#use warnings; # XXX requires 5.6
use Carp qw();
use ExtUtils::Packlist;
use ExtUtils::MakeMaker;
use Config;
use File::Find;
use File::Basename;
use File::Spec;

my $Is_VMS = $^O eq 'VMS';
my $DOSISH = ($^O =~ /^(MSWin\d\d|os2|dos|mint)$/);

require VMS::Filespec if $Is_VMS;

use vars qw($VERSION);
$VERSION = '1.999003';
$VERSION = eval $VERSION;

sub _is_prefix {
    my ($self, $path, $prefix) = @_;
    return unless defined $prefix &amp;&amp; defined $path;

    if( $Is_VMS ) {
        $prefix = VMS::Filespec::unixify($prefix);
        $path   = VMS::Filespec::unixify($path);
    }

    # Unix path normalization.
    $prefix = File::Spec-&gt;canonpath($prefix);

    return 1 if substr($path, 0, length($prefix)) eq $prefix;

    if ($DOSISH) {
        $path =~ s|\\|/|g;
        $prefix =~ s|\\|/|g;
        return 1 if $path =~ m{^\Q$prefix\E}i;
    }
    return(0);
}

sub _is_doc {
    my ($self, $path) = @_;

    my $man1dir = $self-&gt;{':private:'}{Config}{man1direxp};
    my $man3dir = $self-&gt;{':private:'}{Config}{man3direxp};
    return(($man1dir &amp;&amp; $self-&gt;_is_prefix($path, $man1dir))
           ||
           ($man3dir &amp;&amp; $self-&gt;_is_prefix($path, $man3dir))
           ? 1 : 0)
}

sub _is_type {
    my ($self, $path, $type) = @_;
    return 1 if $type eq "all";

    return($self-&gt;_is_doc($path)) if $type eq "doc";
    my $conf= $self-&gt;{':private:'}{Config};
    if ($type eq "prog") {
        return($self-&gt;_is_prefix($path, $conf-&gt;{prefix} || $conf-&gt;{prefixexp})
               &amp;&amp; !($self-&gt;_is_doc($path)) ? 1 : 0);
    }
    return(0);
}

sub _is_under {
    my ($self, $path, @under) = @_;
    $under[0] = "" if (! @under);
    foreach my $dir (@under) {
        return(1) if ($self-&gt;_is_prefix($path, $dir));
    }

    return(0);
}

sub _fix_dirs {
    my ($self, @dirs)= @_;
    # File::Find does not know how to deal with VMS filepaths.
    if( $Is_VMS ) {
        $_ = VMS::Filespec::unixify($_)
            for @dirs;
    }

    if ($DOSISH) {
        s|\\|/|g for @dirs;
    }
    return wantarray ? @dirs : $dirs[0];
}

sub _make_entry {
    my ($self, $module, $packlist_file, $modfile)= @_;

    my $data= {
        module =&gt; $module,
        packlist =&gt; scalar(ExtUtils::Packlist-&gt;new($packlist_file)),
        packlist_file =&gt; $packlist_file,
    };

    if (!$modfile) {
        $data-&gt;{version} = $self-&gt;{':private:'}{Config}{version};
    } else {
        $data-&gt;{modfile} = $modfile;
        # Find the top-level module file in @INC
        $data-&gt;{version} = '';
        foreach my $dir (@{$self-&gt;{':private:'}{INC}}) {
            my $p = File::Spec-&gt;catfile($dir, $modfile);
            if (-r $p) {
                $module = _module_name($p, $module) if $Is_VMS;

                $data-&gt;{version} = MM-&gt;parse_version($p);
                $data-&gt;{version_from} = $p;
                $data-&gt;{packlist_valid} = exists $data-&gt;{packlist}{$p};
                last;
            }
        }
    }
    $self-&gt;{$module}= $data;
}

our $INSTALLED;
sub new {
    my ($class) = shift(@_);
    $class = ref($class) || $class;

    my %args = @_;

    return $INSTALLED if $INSTALLED and ($args{default_get} || $args{default});

    my $self = bless {}, $class;

    $INSTALLED= $self if $args{default_set} || $args{default};


    if ($args{config_override}) {
        eval {
            $self-&gt;{':private:'}{Config} = { %{$args{config_override}} };
        } or Carp::croak(
            "The 'config_override' parameter must be a hash reference."
        );
    }
    else {
        $self-&gt;{':private:'}{Config} = \%Config;
    }

    for my $tuple ([inc_override =&gt; INC =&gt; [ @INC ] ],
                   [ extra_libs =&gt; EXTRA =&gt; [] ])
    {
        my ($arg,$key,$val)=@$tuple;
        if ( $args{$arg} ) {
            eval {
                $self-&gt;{':private:'}{$key} = [ @{$args{$arg}} ];
            } or Carp::croak(
                "The '$arg' parameter must be an array reference."
            );
        }
        elsif ($val) {
            $self-&gt;{':private:'}{$key} = $val;
        }
    }
    {
        my %dupe;
        @{$self-&gt;{':private:'}{LIBDIRS}} =
            grep { $_ ne '.' || ! $args{skip_cwd} }
            grep { -e $_ &amp;&amp; !$dupe{$_}++ }
            @{$self-&gt;{':private:'}{EXTRA}}, @{$self-&gt;{':private:'}{INC}};
    }

    my @dirs= $self-&gt;_fix_dirs(@{$self-&gt;{':private:'}{LIBDIRS}});

    # Read the core packlist
    my $archlib = $self-&gt;_fix_dirs($self-&gt;{':private:'}{Config}{archlibexp});
    $self-&gt;_make_entry("Perl",File::Spec-&gt;catfile($archlib, '.packlist'));

    my $root;
    # Read the module packlists
    my $sub = sub {
        # Only process module .packlists
        return if $_ ne ".packlist" || $File::Find::dir eq $archlib;

        # Hack of the leading bits of the paths &amp; convert to a module name
        my $module = $File::Find::name;
        my $found = $module =~ s!^.*?/auto/(.*)/.packlist!$1!s
            or do {
            # warn "Woah! \$_=$_\n\$module=$module\n\$File::Find::dir=$File::Find::dir\n",
            #    join ("\n",@dirs);
            return;
        };

        my $modfile = "$module.pm";
        $module =~ s!/!::!g;

        return if $self-&gt;{$module}; #shadowing?
        $self-&gt;_make_entry($module,$File::Find::name,$modfile);
    };
    while (@dirs) {
        $root= shift @dirs;
        next if !-d $root;
        find($sub,$root);
    }

    return $self;
}

# VMS's non-case preserving file-system means the package name can't
# be reconstructed from the filename.
sub _module_name {
    my($file, $orig_module) = @_;

    my $module = '';
    if (open PACKFH, $file) {
        while (&lt;PACKFH&gt;) {
            if (/package\s+(\S+)\s*;/) {
                my $pack = $1;
                # Make a sanity check, that lower case $module
                # is identical to lowercase $pack before
                # accepting it
                if (lc($pack) eq lc($orig_module)) {
                    $module = $pack;
                    last;
                }
            }
        }
        close PACKFH;
    }

    print STDERR "Couldn't figure out the package name for $file\n"
      unless $module;

    return $module;
}

sub modules {
    my ($self) = @_;
    $self= $self-&gt;new(default=&gt;1) if !ref $self;

    # Bug/feature of sort in scalar context requires this.
    return wantarray
        ? sort grep { not /^:private:$/ } keys %$self
        : grep { not /^:private:$/ } keys %$self;
}

sub files {
    my ($self, $module, $type, @under) = @_;
    $self= $self-&gt;new(default=&gt;1) if !ref $self;

    # Validate arguments
    Carp::croak("$module is not installed") if (! exists($self-&gt;{$module}));
    $type = "all" if (! defined($type));
    Carp::croak('type must be "all", "prog" or "doc"')
        if ($type ne "all" &amp;&amp; $type ne "prog" &amp;&amp; $type ne "doc");

    my (@files);
    foreach my $file (keys(%{$self-&gt;{$module}{packlist}})) {
        push(@files, $file)
          if ($self-&gt;_is_type($file, $type) &amp;&amp;
              $self-&gt;_is_under($file, @under));
    }
    return(@files);
}

sub directories {
    my ($self, $module, $type, @under) = @_;
    $self= $self-&gt;new(default=&gt;1) if !ref $self;
    my (%dirs);
    foreach my $file ($self-&gt;files($module, $type, @under)) {
        $dirs{dirname($file)}++;
    }
    return sort keys %dirs;
}

sub directory_tree {
    my ($self, $module, $type, @under) = @_;
    $self= $self-&gt;new(default=&gt;1) if !ref $self;
    my (%dirs);
    foreach my $dir ($self-&gt;directories($module, $type, @under)) {
        $dirs{$dir}++;
        my ($last) = ("");
        while ($last ne $dir) {
            $last = $dir;
            $dir = dirname($dir);
            last if !$self-&gt;_is_under($dir, @under);
            $dirs{$dir}++;
        }
    }
    return(sort(keys(%dirs)));
}

sub validate {
    my ($self, $module, $remove) = @_;
    $self= $self-&gt;new(default=&gt;1) if !ref $self;
    Carp::croak("$module is not installed") if (! exists($self-&gt;{$module}));
    return($self-&gt;{$module}{packlist}-&gt;validate($remove));
}

sub packlist {
    my ($self, $module) = @_;
    $self= $self-&gt;new(default=&gt;1) if !ref $self;
    Carp::croak("$module is not installed") if (! exists($self-&gt;{$module}));
    return($self-&gt;{$module}{packlist});
}

sub version {
    my ($self, $module) = @_;
    $self= $self-&gt;new(default=&gt;1) if !ref $self;
    Carp::croak("$module is not installed") if (! exists($self-&gt;{$module}));
    return($self-&gt;{$module}{version});
}

sub debug_dump {
    my ($self, $module) = @_;
    $self= $self-&gt;new(default=&gt;1) if !ref $self;
    local $self-&gt;{":private:"}{Config};
    require Data::Dumper;
    print Data::Dumper-&gt;new([$self])-&gt;Sortkeys(1)-&gt;Indent(1)-&gt;Dump();
}


1;

__END__

=head1 NAME

ExtUtils::Installed - Inventory management of installed modules

=head1 SYNOPSIS

   use ExtUtils::Installed;
   my ($inst) = ExtUtils::Installed-&gt;new( skip_cwd =&gt; 1 );
   my (@modules) = $inst-&gt;modules();
   my (@missing) = $inst-&gt;validate("DBI");
   my $all_files = $inst-&gt;files("DBI");
   my $files_below_usr_local = $inst-&gt;files("DBI", "all", "/usr/local");
   my $all_dirs = $inst-&gt;directories("DBI");
   my $dirs_below_usr_local = $inst-&gt;directory_tree("DBI", "prog");
   my $packlist = $inst-&gt;packlist("DBI");

=head1 DESCRIPTION

ExtUtils::Installed  provides a standard way to find out what core and module
files have been installed.  It uses the information stored in .packlist files
created during installation to provide this information.  In addition it
provides facilities to classify the installed files and to extract directory
information from the .packlist files.

=head1 USAGE

The new() function searches for all the installed .packlists on the system, and
stores their contents. The .packlists can be queried with the functions
described below. Where it searches by default is determined by the settings found
in C&lt;%Config::Config&gt;, and what the value is of the PERL5LIB environment variable.

=head1 METHODS

Unless specified otherwise all method can be called as class methods, or as object
methods. If called as class methods then the "default" object will be used, and if
necessary created using the current processes %Config and @INC.  See the
'default' option to new() for details.


=over 4

=item new()

This takes optional named parameters. Without parameters, this
searches for all the installed .packlists on the system using
information from C&lt;%Config::Config&gt; and the default module search
paths C&lt;@INC&gt;. The packlists are read using the
L&lt;ExtUtils::Packlist&gt; module.

If the named parameter C&lt;skip_cwd&gt; is true, the current directory C&lt;.&gt; will
be stripped from C&lt;@INC&gt; before searching for .packlists.  This keeps
ExtUtils::Installed from finding modules installed in other perls that
happen to be located below the current directory.

If the named parameter C&lt;config_override&gt; is specified,
it should be a reference to a hash which contains all information
usually found in C&lt;%Config::Config&gt;. For example, you can obtain
the configuration information for a separate perl installation and
pass that in.

    my $yoda_cfg  = get_fake_config('yoda');
    my $yoda_inst =
               ExtUtils::Installed-&gt;new(config_override=&gt;$yoda_cfg);

Similarly, the parameter C&lt;inc_override&gt; may be a reference to an
array which is used in place of the default module search paths
from C&lt;@INC&gt;.

    use Config;
    my @dirs = split(/\Q$Config{path_sep}\E/, $ENV{PERL5LIB});
    my $p5libs = ExtUtils::Installed-&gt;new(inc_override=&gt;\@dirs);

B&lt;Note&gt;: You probably do not want to use these options alone, almost always
you will want to set both together.

The parameter C&lt;extra_libs&gt; can be used to specify B&lt;additional&gt; paths to
search for installed modules. For instance

    my $installed =
             ExtUtils::Installed-&gt;new(extra_libs=&gt;["/my/lib/path"]);

This should only be necessary if F&lt;/my/lib/path&gt; is not in PERL5LIB.

Finally there is the 'default', and the related 'default_get' and 'default_set'
options. These options control the "default" object which is provided by the
class interface to the methods. Setting C&lt;default_get&gt; to true tells the constructor
to return the default object if it is defined. Setting C&lt;default_set&gt; to true tells
the constructor to make the default object the constructed object. Setting the
C&lt;default&gt; option is like setting both to true. This is used primarily internally
and probably isn't interesting to any real user.

=item modules()

This returns a list of the names of all the installed modules.  The perl 'core'
is given the special name 'Perl'.

=item files()

This takes one mandatory parameter, the name of a module.  It returns a list of
all the filenames from the package.  To obtain a list of core perl files, use
the module name 'Perl'.  Additional parameters are allowed.  The first is one
of the strings "prog", "doc" or "all", to select either just program files,
just manual files or all files.  The remaining parameters are a list of
directories. The filenames returned will be restricted to those under the
specified directories.

=item directories()

This takes one mandatory parameter, the name of a module.  It returns a list of
all the directories from the package.  Additional parameters are allowed.  The
first is one of the strings "prog", "doc" or "all", to select either just
program directories, just manual directories or all directories.  The remaining
parameters are a list of directories. The directories returned will be
restricted to those under the specified directories.  This method returns only
the leaf directories that contain files from the specified module.

=item directory_tree()

This is identical in operation to directories(), except that it includes all the
intermediate directories back up to the specified directories.

=item validate()

This takes one mandatory parameter, the name of a module.  It checks that all
the files listed in the modules .packlist actually exist, and returns a list of
any missing files.  If an optional second argument which evaluates to true is
given any missing files will be removed from the .packlist

=item packlist()

This returns the ExtUtils::Packlist object for the specified module.

=item version()

This returns the version number for the specified module.

=back

=head1 EXAMPLE

See the example in L&lt;ExtUtils::Packlist&gt;.

=head1 AUTHOR

Alan Burlison &lt;Alan.Burlison@uk.sun.com&gt;

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